DataGrid Update?

  • Thread starter Thread starter Strahimir Antoljak
  • Start date Start date
S

Strahimir Antoljak

I bound a DataGrid to a table (I tried with a DataView too).
I removed the row from the table, and then inserted a new
row at the same row index. the table gets the row to the
right position, but the bound DataGrid does not pick up
and appends the inserted row at the bottom of the grid,
not the right position?!?

Any ideas how to make the DataGrid pick up the changes
done in the table - to show the inserted row at the right
position?

Thanks,
 
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?
 
DataTable.AcceptChanges() does the job. It forces grid to recognize changes
and accepts it as well.


Thanks,
--
Strah @ Langan

Strahimir Antoljak said:
never mind, I got it. Thanks
 
Actually,

DataTable.AcceptChanges() does the job. It forces grid to recognize
changes and accepts it as well.

Thahnks

Strah
 
Strahimir,
Notr that AcceptChanges will also mark all the rows as unchanged so you
won't be able to update back to a database.

Ron Allen
 
Ron,

thanks for your input. I think that I am even aware
of that, but strangely enough, there is no database.

I started developing a Win app that accepts some
user input and based on that it builds Excel table
and populates certain cells with formulas, all based
on the user input as the table can have arbitrary number
of rows and columns, and formulas are dependant on that.

Now the app has grown, and users wanted to save some
of their input, and I actually save all input in the
XML file, not really to a database. So, the AcceptChanges
method does the job for what I needed it.

What you are saying Ron, belongs to a bit more
complicated scenario which would require a bit more
sophisticated UI for the app.

But thanks for the warning anyway.

Happy New Year to all,

Strah
 
Back
Top