Results 1 to 14 of 14

Thread: Datagrid + Generic List + Hide Columns

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Red face Datagrid + Generic List + Hide Columns

    Hi all,

    I have the following situation:

    A WM5/PPC application with a datagrid that have a generic list of object as datasource. The list of Object is actually a List(of Product) where Product class is a value object with some classic properties, prduct id, code, name etc...

    I want to hide some of the fields being displayed and attempted to do so by hiding datagrid columns using DataGridTableStyle. I converted some code from C# and tried to use:


    dim ts as new DataGridTableStyle
    ts.GridColumnStyles.AddColumn(...)


    But it would seem that AddColumn is not a member of GridColumnStyles and it only has the Add method which takes a DataGridColumnStyle as parameter. So i'm a bit here, is there any way to achieve this hiding of columns?
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Datagrid + Generic List + Hide Columns

    Set their widths to 0
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Datagrid + Generic List + Hide Columns

    But I am unable to declare an object as DataGridColumnStyle... This class does not seem to be available, also as mentionned in my first post, GridColumnStyles does not have AddColumn as a member but Add only...
    Last edited by vbud; Aug 12th, 2010 at 05:51 AM.
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  4. #4
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Datagrid + Generic List + Hide Columns

    Use System.Windows.Forms.DataGridColumnStyle
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  5. #5
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Datagrid + Generic List + Hide Columns

    Dim locationCol1 As New DataGridTextBoxColumn
    Dim locationCol2 As New DataGridTextBoxColumn
    .....
    Dim aGridTableStyle2 As New DataGridTableStyle
    aGridTableStyle2.MappingName = "Source"
    .....
    With locationCol1
    .MappingName = "Reg No"
    .Width = 70
    .NullText = ""
    End With

    With locationCol2
    .MappingName = "Status"
    .Width = 0
    .NullText = ""
    End With
    ....
    With aGridTableStyle2.GridColumnStyles
    .Add(locationCol1)
    .Add(locationCol2)
    End With

    ....
    dgPermits.TableStyles.Add(aGridTableStyle2)

    Should help
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  6. #6
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Datagrid + Generic List + Hide Columns

    Hmm actually i found that using width=-1 actually hide the column, width=0 didn't do it right.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  7. #7

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Datagrid + Generic List + Hide Columns

    Does not seem to work, on thing I forgot to mention is that I have a generic List(Of Object) as datasource. Each object is in fact a simple Product Class object. From the code you pasted petevick the compiler does not complain, but then i cannot see the column header text as per your code. Any ideas?

    Code as below:

    Code:
                Cursor.Current = Cursors.WaitCursor
                Dim objList As List(Of Object) = objProductBO.GetProduct()
                Cursor.Current = Cursors.Default
    
                Dim locationCol1 As New DataGridTextBoxColumn
                Dim locationCol2 As New DataGridTextBoxColumn
    
                Dim aGridTableStyle2 As New DataGridTableStyle
                aGridTableStyle2.MappingName = "Source"
    
                With locationCol1
                    .MappingName = "Reg No"
                    .Width = 70
                    .NullText = ""
                End With
    
                With locationCol2
                    .MappingName = "Status"
                    .Width = 0
                    .NullText = ""
                End With
    
                With aGridTableStyle2.GridColumnStyles
                    .Add(locationCol1)
                    .Add(locationCol2)
                End With
    
                dtgProduct.TableStyles.Clear()
                dtgProduct.TableStyles.Add(aGridTableStyle2)
    
                If Not objList Is Nothing Then
                    dtgProduct.DataSource = objList
                End If
    Whether I assign the datasource before or after the column formating code does not make any difference
    Last edited by vbud; Aug 15th, 2010 at 12:17 PM.
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  8. #8
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Datagrid + Generic List + Hide Columns

    Hi,
    I seem to remember that code doesn't show headings on the grid. Take a look at the 'headertext' property (I think )
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  9. #9

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Datagrid + Generic List + Hide Columns

    Still does not work... Could it be that the DataGridTextBoxColumn object is unable to map property name of an object with the MappingName when a List of Object is bound to a datagrid?
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  10. #10

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Datagrid + Generic List + Hide Columns

    Here's a sample code I am trying to test:

    Employee Class:
    Code:
    Public Class Employee
    
        Private empId As Integer
        Public Property EmployeeId() As Integer
            Get
                Return empId
            End Get
            Set(ByVal value As Integer)
                empId = value
            End Set
        End Property
    
    
        Private empCode As String
        Public Property EmployeeCode() As String
            Get
                Return empCode
            End Get
            Set(ByVal value As String)
                empCode = value
            End Set
        End Property
    
        Private empName As String
        Public Property EmployeeName() As String
            Get
                Return empName
            End Get
            Set(ByVal value As String)
                empName = value
            End Set
        End Property
    
    End Class
    Form Code:
    Code:
        Private empList As New List(Of Employee)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim locationCol1 As New DataGridTextBoxColumn
            Dim locationCol2 As New DataGridTextBoxColumn
    
            Dim aGridTableStyle2 As New DataGridTableStyle
            aGridTableStyle2.MappingName = "Source"
    
            With locationCol1
                .MappingName = "EmployeeId"
                .Width = -1
                .NullText = ""
            End With
    
            With aGridTableStyle2.GridColumnStyles
                .Add(locationCol1)
            End With
    
            For index As Integer = 1 To 10
                Dim emp As New Employee
                With emp
                    .EmployeeId = index
                    .EmployeeCode = String.Concat("EM", index.ToString)
                    .EmployeeName = String.Concat("Test Employee ", index.ToString)
                End With
                empList.Add(emp)
            Next
    
            DataGrid1.DataSource = empList
            DataGrid1.TableStyles.Clear()
            DataGrid1.TableStyles.Add(aGridTableStyle2)
    
        End Sub
    I suspect that the property name is not the same as the Mapping Name. Any suggestions?
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  11. #11
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Datagrid + Generic List + Hide Columns

    Couldn't test that code atm but could you try:
    aGridTableStyle2.MappingName = empList.toString()
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  12. #12
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Datagrid + Generic List + Hide Columns

    Here's the code i'm using for the same purpose as you...
    Note that i have 2 custom classes called DataGridColoredTextBoxColumn and DataGridCustomTextBoxColumn, you might want to ignore that.

    vb Code:
    1. Private Sub formataGrelhaEntidades()
    2.         Try
    3.             Dim aGridTableStyle2 As New DataGridTableStyle
    4.             Dim locationCol1 As New DataGridColoredTextBoxColumn
    5.             Dim locationCol2 As New DataGridCustomTextBoxColumn
    6.             Dim locationCol3 As New DataGridTextBoxColumn
    7.             Dim locationCol4 As New DataGridTextBoxColumn
    8.             Dim locationCol5 As New DataGridTextBoxColumn
    9.  
    10.             aGridTableStyle2.MappingName = myDataset.ToString
    11.             Me.DataGridEntidades.RowHeadersVisible = False
    12.             Me.DataGridEntidades.ColumnHeadersVisible = True
    13.             With locationCol1
    14.                 .backgroundColor = Color.LightSteelBlue
    15.                 .ForeColor = Color.Black
    16.                 .MappingName = "Cod"
    17.                 .HeaderText = "Cod"
    18.                 .Width = 44
    19.                 .NullText = ""
    20.             End With
    21.             With locationCol2
    22.                 .Owner = Me.DataGridEntidades
    23.                 .Alignment = HorizontalAlignment.Left
    24.                 .AlternatingBackColor = Color.LightSteelBlue
    25.                 .MappingName = "Nome"
    26.                 .HeaderText = "Empresa"
    27.                 .ReadOnly = True
    28.                 .Width = Screen.PrimaryScreen.WorkingArea.Width - locationCol1.Width - 16
    29.                 .NullText = ""
    30.             End With
    31.             With locationCol3
    32.                 .MappingName = "Morada"
    33.                 .HeaderText = "Morada"
    34.                 .Width = -1
    35.                 .NullText = ""
    36.             End With
    37.             With locationCol4
    38.                 .MappingName = "CodPostal"
    39.                 .HeaderText = "CodPostal"
    40.                 .Width = -1
    41.                 .NullText = ""
    42.             End With
    43.             With locationCol5
    44.                 .MappingName = "Contacto"
    45.                 .HeaderText = "Contacto"
    46.                 .Width = -1
    47.                 .NullText = ""
    48.             End With
    49.  
    50.             With aGridTableStyle2.GridColumnStyles
    51.                 .Add(locationCol1)
    52.                 .Add(locationCol2)
    53.                 .Add(locationCol3)
    54.                 .Add(locationCol4)
    55.                 .Add(locationCol5)
    56.             End With
    57.             With DataGridEntidades
    58.                 .BackColor = Color.AliceBlue
    59.                 .BackgroundColor = Color.AliceBlue
    60.                 .GridLineColor = Color.RoyalBlue
    61.                 .HeaderBackColor = Color.SteelBlue
    62.                 .SelectionBackColor = Color.LightCoral
    63.                 .SelectionForeColor = Color.DarkBlue
    64.             End With
    65.             DataGridEntidades.TableStyles.Clear()
    66.             DataGridEntidades.TableStyles.Add(aGridTableStyle2)
    67.         Catch ex As Exception
    68.             '
    69.         End Try
    70.     End Sub
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  13. #13

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Datagrid + Generic List + Hide Columns

    aGridTableStyle2.MappingName = empList.toString() does not do much...
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  14. #14
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Datagrid + Generic List + Hide Columns

    Have you ever try to use datasets? They use to help, they are fast reading data too.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width