There is a lot of code spread in 4 projects making a solution.
I'll try to post code snippets relevant to what I want to achieve.
Public Sub CreateIntesectionTable_
(ByRef ds As System.Data.DataSet, ByRef dg As
Windows.Forms.DataGrid)
' Call custom function to add a DataTable to the DataSet
Dim sTableName As String = "IntesectionData"
Dim dt As DataTable = AddTableToDataSet(ds, sTableName)
' Call custom function to add DataColumns to the DataTable created
above.
AddColumnToTable(dt, "Intersection_Name", _
"Intersection_Name", System.Type.GetType("System.String"), _
"", True, False, False, False, 0, 1)
AddColumnToTable(dt, "Condition", "Condition", _
System.Type.GetType("System.String"), "", True, False, False,
False, 0, 1)
'... Add more columns
' Represent data from DataTable in DataGrid
ShowTableInDataGrid(dt, dg)
End Sub
Public Sub ShowTableInDataGrid(ByRef dt As DataTable, ByRef dg As
System.Windows.Forms.DataGrid)
Dim dv As New DataView()
dv = New DataView(dt)
dg.DataSource = dv
End Sub
There is a button on the form that a row from the DataTable. At the same
time
it captures a row index of the deleted row. When a row is deleted from the
table,
it automatically disappears from the DataGrid. The code looks something like
this:
Private iRowIndex as Integer = 0
Private Sub btnEdit_Click_
(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnEdit.Click
' .... Some code here ....
iRowIndex = Me.dgNetworkSummary.CurrentRowIndex
ds.Tables("IntesectionData").Rows(iRowIndex).Delete()
' .... Some code here ....
End Sub
There is also a button that appends new row to the DataTable
Private Sub btnSubmit_Click_
(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnSubmit.Click
' Some code here...
Dim rowVals(2) As Object
rowVals(0) = someValue0
rowVals(1) = someValue1
rowVals(2) = someValue2
Dim rc As DataRowCollection
Dim myNewRow As DataRow
rc = dt.Rows 'Note: dt = ds.Tables("IntesectionData")
myNewRow = rc.Add(rowVals)
' Some code here...
End Sub
The above procedure adds a new row to the DataTable (IntesectionData). The
row automaticaly appears at the bottom of DataGrid too, which is expected
behavior.
Now, I want to change the btnSubmit_Click() procedure to insert a
new row at the iRowIndex position in the DataTable and show
that row in the right position in the DataGrid as well. Here
is modified procedure.
Private Sub btnSubmit_Click_
(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnSubmit.Click
' Some code here...
Dim insertedRow As DataRow
insertedRow = dt.NewRow 'Note: dt = ds.Tables("IntesectionData")
insertedRow(0) = Value0
insertedRow(1) = Value1
insertedRow(2) = Value2
dt.Rows.InsertAt(insertedRow, iRowIndex)
' This loops proves that new row is inserted
' on the right position in the DataTable, however,
' in the DataGrid, the new row is appended at the
' bottom ???
For Each insertedRow In dt.Rows
Debug.WriteLine(insertedRow(0))
Next
' Some code here...
End Sub
The new, modified btnSubmit_Click() procedure inserts the new
row at the right location in the DataTable, however, in
the DataGrid, the new row is appended at the bottom.
My question is, if there is a way to show the new row
at the right position in the DataGrid too, or in other
words, to force the DataGrid to display the DataTable
correctly? So the DataGrid updates on the fly?
I hope I was clear enough, but please let me know if
you need more info. Thanks for your help.
--
Strah @ Langan
scorpion53061 said:
Can I see the code you are using?