Geeks With Blogs
Everything IT Programmer, SQL and Server Admin

Silly me, I thought that manipulating columns in a datagrid control would be real easy.  However, when you start looking for the columns collection, it's not easy to find.  Where is it?  Located deep down in the TableStyles collection of DataGridTableStyles which contains a collection of DataGridColumns.  You can have a DataGridBoolColumn or a DataGridTextBoxColumn.

The DataGridTextBoxColumn is what you use to hold any value that is not a Boolean value I guess.  You can then set various properties of this column such as its width, readonly and mappingname that will allow you to control some of the appearance of the control and what values are displayed in the column.

Thus far I have determined that when you set one column to readonly, they all become readonly.  Strange.  At any rate, you can now control what columns you want displayed in a datagrid by creating a the necessary columns, adding them to a DataGridTableStyle and then adding the style to a datagrid.  Here is some code that shows this.

Dim naCultureInfo As CultureInfo
naCultureInfo =
New CultureInfo("en-CA")
Dim ts1 As New DataGridTableStyle
Dim colGroup As New DataGridTextBoxColumn
Dim colNotes As New DataGridTextBoxColumn
Dim colUnit As New DataGridTextBoxColumn
Dim colPrice As New DataGridTextBoxColumn
Dim colTotal As New DataGridTextBoxColumn
Dim colQty As New DataGridTextBoxColumn

ts1.MappingName = dsOne.Tables(1).ToString

With colGroup
          .HeaderText = "Group"
          .MappingName = "Group"
          .ReadOnly =
True
          .Width = 200
End With

With colNotes
          .HeaderText = "Notes"
          .MappingName = "Notes"
          .ReadOnly =
True
         
.Width = 430
End With

With colUnit
          .HeaderText = "Unit"
          .MappingName = "Unit"
          .ReadOnly =
True
         
.Width = 60
End With

With colPrice
            .HeaderText = "Price"
            .MappingName = "Price"
            .ReadOnly =
True
           
.Width = 60
            .FormatInfo = naCultureInfo
            .Format = "c"
End With

With colQty
             .HeaderText = "Qty"
             .MappingName = "Quantity"
             .Width = 60
End With

With colTotal
              .HeaderText = "Total"
              .MappingName = "Total"
              .ReadOnly =
True
             
.Width = 79
              .FormatInfo = naCultureInfo
              .Format = "c"
End With

ts1.GridColumnStyles.Add(colGroup)
ts1.GridColumnStyles.Add(colNotes)
ts1.GridColumnStyles.Add(colUnit)
ts1.GridColumnStyles.Add(colPrice)
ts1.GridColumnStyles.Add(colQty)
ts1.GridColumnStyles.Add(colTotal)

ts1.RowHeadersVisible = False

dgQuoteDetails.TableStyles.Add(ts1)

Posted on Thursday, April 29, 2004 12:34 PM | Back to top


Comments on this post: Datagrid Control Woes

# re: Datagrid Control Woes
Requesting Gravatar...
Hi thanx but pls specify whether it is for winforms or webforms
Left by Bhavesh on May 22, 2005 5:02 PM

# re: Datagrid Control Woes
Requesting Gravatar...
It is for Winforms
Left by Gerry on May 23, 2005 3:31 PM

Your comment:
 (will show your gravatar)


Copyright © Gerry O'Brien | Powered by: GeeksWithBlogs.net