Dynamically creating a DataGridTableStyle

  • Thread starter Thread starter Jeff Molby
  • Start date Start date
J

Jeff Molby

This is frustrating me. I have to create a new table at runtime, and
I'm able to, but it doesn't appear. The datagrid always shows the default
tablestyle, no matter what I do. Here's my code.

<Snip>

Dim col As Windows.Forms.DataGridColumnStyle
Dim ts As New Windows.Forms.DataGridTableStyle

'Creates by Business Object
dcTaxTypes = New InventoryBO.ProductTaxTypes
dcTaxTypes.ConnectionString = CONNECT_STRING
dcTaxTypes.SelectFilter =
InventoryDC.ProductTaxTypesDC.SelectFilters.ListBox
dcTaxTypes.OrderByFilter =
InventoryDC.ProductTaxTypesDC.OrderByFilters.Description
dcTaxTypes.Open()

ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "ProductID"
col.Width = 100
col.HeaderText = "ID"
ts.GridColumnStyles.Add(col)

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "TaxAmount"
col.Width = 100
col.HeaderText = "Amount"
ts.GridColumnStyles.Add(col)

Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = dcDetails
Me.DataGrid1.Refresh()

</Snip>

This code executes without throwing any errors, but it does absolutely
nothing to my datagrid. It shows all 4 of the columns that the default
tablestyle has, rather than just the two columns that the new tablestyle
has.

You're a hero if you can help me!

Jeff
 
Hi Jeff,
ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

What is the GetMappingName function? How it works? It might produce an
incorrect mapping name.
The correct mapping name should correspond to the TableName property value
on the bound DataTable instance.
Me.DataGrid1.DataSource = dcDetails

I believe the "dcDetails" variable is not initialized in the code snippet.
Ensure it is the same as dcTaxTypes.DataSet.Tables(0). The grid would not be
able to associate the created table style with the bound data source
otherwise.

You might also with to use the SetDataBinding method instead of assigning
the DataSource property.
 
Here is the GetMappingName function. It is returning the proper name.


Private Function GetMappingName(ByVal src As Object) As String
Dim list As IList = Nothing
Dim t As Type = Nothing

If TypeOf (src) Is Array Then
t = src.GetType()
list = CType(src, IList)
Else
If TypeOf src Is IListSource Then
src = CType(src, IListSource).GetList()
End If

If TypeOf src Is IList Then
t = src.GetType()
list = CType(src, IList)
Else
Return ""
End If
End If

If TypeOf list Is ITypedList Then
Return CType(list, ITypedList).GetListName(Nothing)
Else
Return t.Name
End If
End Function

Dmitriy Lapshin said:
Hi Jeff,
ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

What is the GetMappingName function? How it works? It might produce an
incorrect mapping name.
The correct mapping name should correspond to the TableName property value
on the bound DataTable instance.
Me.DataGrid1.DataSource = dcDetails

I believe the "dcDetails" variable is not initialized in the code snippet.
Ensure it is the same as dcTaxTypes.DataSet.Tables(0). The grid would not be
able to associate the created table style with the bound data source
otherwise.

You might also with to use the SetDataBinding method instead of assigning
the DataSource property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jeff Molby said:
This is frustrating me. I have to create a new table at runtime, and
I'm able to, but it doesn't appear. The datagrid always shows the default
tablestyle, no matter what I do. Here's my code.

<Snip>

Dim col As Windows.Forms.DataGridColumnStyle
Dim ts As New Windows.Forms.DataGridTableStyle

'Creates by Business Object
dcTaxTypes = New InventoryBO.ProductTaxTypes
dcTaxTypes.ConnectionString = CONNECT_STRING
dcTaxTypes.SelectFilter =
InventoryDC.ProductTaxTypesDC.SelectFilters.ListBox
dcTaxTypes.OrderByFilter =
InventoryDC.ProductTaxTypesDC.OrderByFilters.Description
dcTaxTypes.Open()

ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "ProductID"
col.Width = 100
col.HeaderText = "ID"
ts.GridColumnStyles.Add(col)

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "TaxAmount"
col.Width = 100
col.HeaderText = "Amount"
ts.GridColumnStyles.Add(col)

Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = dcDetails
Me.DataGrid1.Refresh()

</Snip>

This code executes without throwing any errors, but it does absolutely
nothing to my datagrid. It shows all 4 of the columns that the default
tablestyle has, rather than just the two columns that the new tablestyle
has.

You're a hero if you can help me!

Jeff
 
Dmitriy Lapshin said:
I believe the "dcDetails" variable is not initialized in the code snippet.
Ensure it is the same as dcTaxTypes.DataSet.Tables(0). The grid would not be
able to associate the created table style with the bound data source
otherwise.

It wasn't in the snippet, but it is getting initialized and the table is
being populated properly. It just isn't displaying the proper layout.
You might also with to use the SetDataBinding method instead of assigning
the DataSource property.

I will give this a try.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jeff Molby said:
This is frustrating me. I have to create a new table at runtime, and
I'm able to, but it doesn't appear. The datagrid always shows the default
tablestyle, no matter what I do. Here's my code.

<Snip>

Dim col As Windows.Forms.DataGridColumnStyle
Dim ts As New Windows.Forms.DataGridTableStyle

'Creates by Business Object
dcTaxTypes = New InventoryBO.ProductTaxTypes
dcTaxTypes.ConnectionString = CONNECT_STRING
dcTaxTypes.SelectFilter =
InventoryDC.ProductTaxTypesDC.SelectFilters.ListBox
dcTaxTypes.OrderByFilter =
InventoryDC.ProductTaxTypesDC.OrderByFilters.Description
dcTaxTypes.Open()

ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "ProductID"
col.Width = 100
col.HeaderText = "ID"
ts.GridColumnStyles.Add(col)

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "TaxAmount"
col.Width = 100
col.HeaderText = "Amount"
ts.GridColumnStyles.Add(col)

Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = dcDetails
Me.DataGrid1.Refresh()

</Snip>

This code executes without throwing any errors, but it does absolutely
nothing to my datagrid. It shows all 4 of the columns that the default
tablestyle has, rather than just the two columns that the new tablestyle
has.

You're a hero if you can help me!

Jeff
 
Nevermind, I'm an idiot. The MappingName was wrong.


Jeff Molby said:
Here is the GetMappingName function. It is returning the proper name.


Private Function GetMappingName(ByVal src As Object) As String
Dim list As IList = Nothing
Dim t As Type = Nothing

If TypeOf (src) Is Array Then
t = src.GetType()
list = CType(src, IList)
Else
If TypeOf src Is IListSource Then
src = CType(src, IListSource).GetList()
End If

If TypeOf src Is IList Then
t = src.GetType()
list = CType(src, IList)
Else
Return ""
End If
End If

If TypeOf list Is ITypedList Then
Return CType(list, ITypedList).GetListName(Nothing)
Else
Return t.Name
End If
End Function

Dmitriy Lapshin said:
Hi Jeff,


What is the GetMappingName function? How it works? It might produce an
incorrect mapping name.
The correct mapping name should correspond to the TableName property value
on the bound DataTable instance.


I believe the "dcDetails" variable is not initialized in the code snippet.
Ensure it is the same as dcTaxTypes.DataSet.Tables(0). The grid would
not
be
able to associate the created table style with the bound data source
otherwise.

You might also with to use the SetDataBinding method instead of assigning
the DataSource property.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jeff Molby said:
This is frustrating me. I have to create a new table at runtime, and
I'm able to, but it doesn't appear. The datagrid always shows the default
tablestyle, no matter what I do. Here's my code.

<Snip>

Dim col As Windows.Forms.DataGridColumnStyle
Dim ts As New Windows.Forms.DataGridTableStyle

'Creates by Business Object
dcTaxTypes = New InventoryBO.ProductTaxTypes
dcTaxTypes.ConnectionString = CONNECT_STRING
dcTaxTypes.SelectFilter =
InventoryDC.ProductTaxTypesDC.SelectFilters.ListBox
dcTaxTypes.OrderByFilter =
InventoryDC.ProductTaxTypesDC.OrderByFilters.Description
dcTaxTypes.Open()

ts.MappingName = GetMappingName(dcTaxTypes.DataSet.Tables(0)) ' Sets the
Mapping Name

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "ProductID"
col.Width = 100
col.HeaderText = "ID"
ts.GridColumnStyles.Add(col)

col = New Windows.Forms.DataGridTextBoxColumn
col.MappingName = "TaxAmount"
col.Width = 100
col.HeaderText = "Amount"
ts.GridColumnStyles.Add(col)

Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = dcDetails
Me.DataGrid1.Refresh()

</Snip>

This code executes without throwing any errors, but it does absolutely
nothing to my datagrid. It shows all 4 of the columns that the default
tablestyle has, rather than just the two columns that the new tablestyle
has.

You're a hero if you can help me!

Jeff
 
Back
Top