Hi Paszt,
Here is the code snippet to bind the dataGrid with dataTable.
-------------------------------------------
Private Sub DrawSizeGrid()
Dim i As Integer = 0
Dim strSizeHead As String
If dsSalesOrderSizes.Tables("dtSalesOrderLotSizeConfig").Rows.Count = 0 Then
grdSizes.Enabled = False
btnCalcQuantities.Enabled = False
grdSizes.CaptionText = "Please configure the sizes for this lot."
Exit Sub
End If
grdSizes.DataBindings.Clear()
grdSizes.Refresh()
If Not dsSalesOrderSizes.Tables("dtSizes") Is Nothing Then
dsSalesOrderSizes.Tables.Remove("dtSizes")
End If
dtSizes = New DataTable("dtSizes")
dtSizes.Columns.Add("Type", Type.GetType("System.String"))
If dsSalesOrderSizes.Tables("dtSalesOrderLotSizeConfig").Rows.Count > 0 Then
With dsSalesOrderSizes.Tables("dtSalesOrderLotSizeConfig")
For i = 0 To .Rows.Count - 1
strSizeHead = CType(.Rows(i)("SizeDescription"), String)
dtSizes.Columns.Add(strSizeHead, Type.GetType("System.Int64"))
Next
End With
End If
dsSalesOrderSizes.Tables.Add(dtSizes)
With dsSalesOrderSizes.Tables("dtSizes")
Dim drRow As DataRow
drRow = .NewRow()
drRow("Type") = "Ratio"
For i = 1 To .Columns.Count - 1
drRow(i) = 0
Next
.Rows.Add(drRow)
drRow = .NewRow()
drRow("Type") = "Quantity"
For i = 1 To .Columns.Count - 1
drRow(i) = 0
Next
.Rows.Add(drRow)
End With
grdSizes.Enabled = True
grdSizes.DataBindings.Clear()
grdSizes.SetDataBinding(dsSalesOrderSizes, "dtSizes")
'Prevent AddNew Row option thru dataview
Dim cm As CurrencyManager = CType(Me.BindingContext(grdSizes.DataSource, grdSizes.DataMember), CurrencyManager)
CType(cm.List, DataView).AllowNew = False
grdSizes.CaptionText = "Sizes"
Dim dgtsConfig As New DataGridTableStyle
dgtsConfig.MappingName = "dtSizes"
With grdSizes
.TableStyles.Clear()
.TableStyles.Add(dgtsConfig)
SetColCaption(dgtsConfig, "Type", "")
End With
End Sub
-------------------------------------------
You can see that the dataTable "dtSizes" can have 2 columns, 3 columns or n Columns depending upon the number of rows present in dsSalesOrderSizes.Tables("dtSalesOrderLotSizeConfig") dataTable.
When I display the number of rows in dsSalesOrderSizes.Tables("dtSalesOrderLotSizeConfig") dataTable, I can see correct number of rows but it cannot be shown if grid has already been bound with the dataTable with different number of columns.
Scenario:
1. On one instance, if number of rows are 2, there would be 2 columns (since dtSizes are dynamically created with number of columns equivalent to number of rows in other dataTable.
2. On second instance, may be on changeRow of master grid, I re-draw the Sizes grid. Though the number of columns have successfully been changed/re-assigned, but the dataGrid displays the old structure (say, 2 columns).
I used following statement to clear the dataBinding:
grdSizes.DataBindings.Clear()
By altered dataTable, I wanted to say the modified structure of dataTable (check above code for dtSizes).
Ideally, once the dataGrid binding has been cleared, it should show the new structure when we bind it with re-structured dataTable. I'm not sure where I'm wrong.
Would appreciate if you can iron out the problem. Thanks.