datagrid column width and row color

SteveInBeloit

Well-known member
Joined
May 22, 2006
Messages
132
Programming Experience
10+
VS 2003 VB .NET

I am currently trying to accomplish two things.
1. In Code, trying to set the column width
2. Trying to change the color of the row based on the value of one cell.

For the width, I thought I could do it like this:

VB.NET:
Dim ts As DataGridTableStyle = New DataGridTableStyle
Dim daGrid1 As New SqlDataAdapter(Me.SqlCommand1)
dsGrid1 = New DataSet
dsGrid1.Clear()
daGrid1.Fill(dsGrid1, "top")
DataGrid1.DataSource = dsGrid1.Tables(0).DefaultView
DataGrid1.TableStyles.Add(ts)
DataGrid1.TableStyles(0).GridColumnStyles(1).Width = 5
This does not effect the grid at all.

2. I don't know where to start, can't find a walkthrough or article on just this.

I am needing to do both of these at run time, not design time.

Any thoughts?
Thanks
 
Last edited by a moderator:
I think you have got the order a bit mixed-up.

1. declare a tablestyle.
2.declare columnstyles.
3. size column styles.
4. add columns to tablestyle.
5. add tablestyle to datagrid.
 
Got it now, incase anyone needs an example:

VB.NET:
Dim myConnection As New SqlConnection(strConn)
Dim dts As DataGridTableStyle = New DataGridTableStyle
Dim daGrid1 As New SqlDataAdapter(Me.SqlCommand1)
dsGrid1 = New DataSet
 
dsGrid1.Clear()
daGrid1.Fill(dsGrid1, "top")
'dts.MappingName = dsGrid1.Tables("top").TableName
dts.MappingName = "top"
Dim colPrior As New DataGridTextBoxColumn
With colPrior
.MappingName = "Priority"
.HeaderText = "Priority"
.Width = 40
End With
 
dts.GridColumnStyles.Add(colPrior)
 
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(dts)
DataGrid1.DataSource = dsGrid1.Tables(0).DefaultView
 
Last edited by a moderator:
Back
Top