Data Grid Dynamic Binding Problem.

asjad

New member
Joined
Jan 7, 2005
Messages
2
Programming Experience
1-3
When I dynamically bind the datagrid (in VB.Net) with dataTable, I can see all columns of dataTable. When I clear the dataBinding and re-bind with altered dataTable it still show the old number of columns. For instance, if the gird was initially bound with 2 columns, it still show 2 columns even the number of columns are 4. If I close the form, re-open it and populate the dataTable having 4 columns, I get the 4 columns in dataGrid. but again, if I re-bind with 2 column dataTable I don't see 2 columns, rather I see the old structure, i.e. 4 columns.

I am stucked and need to quickly get out of it. Would appreciate if anyone can help me out from this issue. Thanks in advance.
 
I tried to recreate your problem to no avail.
How are you clearing the dataBindings? What do you mean altered dataTable?
Perhaps you could show some code.
 
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.

 
Back
Top