Datagrid columns

  • Thread starter Thread starter Eregnon
  • Start date Start date
E

Eregnon

Guest
Theres probably a simple answer to this, but I just dont see it.

I have a datagrid. I have a data set with a table. I want to only display selected columns from the table in the grid. How do I do that? Every method I see in the help examples displays the entire table.
 
Hi Eregnon

I thought it would be simple as well, but here is what I did.

Firstly, dont set up the grid properties DataSource & TableSource. I found it easier to set them in code.

Ive copied the code from my form (Saves on spelling mistakes)

Ive got a grid called grdHotelGroups, but the code should be self explanatory (I think)

You have to set up a DataGridTableStyle and a DataGridTextBoxColumn

I copied the code from MSDN and changed it to suit. I left the comments as they make more sense than me.

Hope this helps


Code:
Private Sub FormatHGGrid()

        With Me.grdHotelGroups
            .DataSource = HotelGroupData
            .DataMember = "tblHotelGroup"

            .BorderStyle = BorderStyle.None
            .CaptionFont = New Font("Tahoma", 10.0!, FontStyle.Bold)
            .CaptionText = "Hotel Groups"
            .Font = New Font("Tahoma", 8.0!)
        End With

         Put as much of the formatting as possible here.
        Dim grdTableStyle1 As New DataGridTableStyle()
        With grdTableStyle1
            .HeaderFont = New Font("Tahoma", 8.0!, FontStyle.Bold)
            .RowHeadersVisible = False
             Do not forget to set the MappingName property. 
             Without this, the DataGridTableStyle properties
             and any associated DataGridColumnStyle objects
             will have no effect.
            .MappingName = "tblHotelGroup"
            .PreferredColumnWidth = 125
            .PreferredRowHeight = 15
        End With

         Format each column that you want to appear in the DataGrid.
         In most cases, the DataGridTextBoxColumn class is appropriate.
         However, you can also use the DataGridBoolColumn class. Both
         of these extend the MustInherit DataGridColumnStyle class. Notice
         that the column style properties available to you are more limited
         than those for the table style. For example, you cannot change
         the color of an individual column.
        Dim grdColStyle1 As New DataGridTextBoxColumn()
        With grdColStyle1
            .HeaderText = ""
            .MappingName = "intHotelGroupID"
            .Width = 0
            .ReadOnly = True
        End With

        Dim grdColStyle2 As New DataGridTextBoxColumn()
        With grdColStyle2
            .HeaderText = "Hotel Groups"
            .MappingName = "vstrHotelGroupName"
            .Width = Me.grdHotelGroups.Width
        End With

         Add the style objects to the table styles collection of 
         column styles. Without this the styles do not take effect.        
        grdTableStyle1.GridColumnStyles.AddRange(New DataGridColumnStyle() {grdColStyle2})
        grdHotelGroups.TableStyles.Add(grdTableStyle1)
    End Sub
 
Last edited by a moderator:
Hmmm...glad to see vb.net promotes such efficient coding. :)

Thanks for your help. Ill give this a shot.
 
Back
Top