Hiding/sizing columns in data grid
I have a data grid that is bound to a relationship of two tables in a dataset. The common field in the relationship is classId. When I view the data in the data grid I do not want to see the classId field but if I do not include the field in my select statement I get an error when I try to form the relationship. Is there anyway to hide a field/column in a data grid? ALong this line of questions, can the width of the data grid columns be dynamically set to fit the data in each column.
I can include code samples if needed.
Re: Hiding/sizing columns in data grid
yeah you can set the size of the column width of the datagrid. try to use datagridtablestyle.
search in this forum and you can find a lot of it.:)
Re: Hiding/sizing columns in data grid
Quote:
try to use datagridtablestyle.
That got me close but I'm still having problems. Here is what I'm trying:
VB Code:
Dim tableStyle As New DataGridTableStyle
Dim colStyle As DataGridColumnStyle
daClass.Fill(ds, "Classes")
daHull.Fill(ds, "Hulls")
ds.Relations.Add("ClassRel", _
ds.Tables("Classes").Columns("classId"), _
ds.Tables("Hulls").Columns("classId"))
Me.dgHulls.SetBindings(ds,Classes.ClassRel")
tableStyle.MappingName = "Classes.ClassRel"
Me.dgHulls.TableStyles.Add(tableStyle)
colStyle = tableStyle.GridColumnStyles.Item("classId")
colStyle.Width = 0
When I try to run this I get an error on colStyle.Width = 0:
Object reference not set to an instance of an object.
The debugger shows that colStyle has a value of "Nothing". Can anyone explain what I'm doing wrong?
Re: Hiding/sizing columns in data grid
After looking at few more examples I've changed my code but now I get a different error (blue squiggle line): Option Strict On disallows implicit conversions from 'System.Windows.Forms.DataGridColumnStyle' to 'System.Windows.Forms.DataGridTextBoxColumn'
VB Code:
[B]Dim tableStyle As DataGRidTableStyle = New DataGridTableStyle[/B]
[B]Dim colStyle As DataGridTextBoxColumn[/B]
daClass.Fill(ds, "Classes")
daHull.Fill(ds, "Hulls")
ds.Relations.Add("ClassRel", _
ds.Tables("Classes").Columns("classId"), _
ds.Tables("Hulls").Columns("classId"))
Me.dgHulls.SetBindings(ds,Classes.ClassRel")
tableStyle.MappingName = "Classes.ClassRel"
Me.dgHulls.TableStyles.Add(tableStyle)
colStyle = tableStyle.GridColumnStyles.Item("classId")
colStyle.Width = 0
I can turn off Option Strict and the blue line goes away but then I get the same error in my previous post. What am I missing?
Re: Hiding/sizing columns in data grid
try this way.
VB Code:
dim dgt as new DataGridTableStyle()
dim dgtb as new DataGridTextBoxColumn()
dgtb.MappingName="classid"
dgtb.Width=0
dgt.GridColumnStyles.Add(dgtb)
Re: Hiding/sizing columns in data grid
mar_zim
Thanks for your help. Your method got rid of the errors but the colulmn widths are not changing. Everything works as expected when I use this code in on a form where the table relationship is accomplished using the SQL join command and the data source is assigned using the dataMember property. Maybe one or both of the following statements are complicating things.
VB Code:
ds.Relations.Add("ClassRel", _
ds.Tables("Classes").Columns("classId"), _
ds.Tables("Hulls").Columns("classId"))
Me.dgHulls.SetBindings(ds,Classes.ClassRel")
Any thoughts?