469,609 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

DataGrid Update?

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,
--
Strah @ Langan
Nov 20 '05 #1
8 1859
Can I see the code you are using?

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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,
--
Strah @ Langan

Nov 20 '05 #2
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).Delet e()

' .... 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" <sc****************************@yahoo.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Can I see the code you are using?

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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,
--
Strah @ Langan


Nov 20 '05 #3
never mind, I got it. Thanks
--
Strah @ Langan

"scorpion53061" <sc****************************@yahoo.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Can I see the code you are using?

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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,
--
Strah @ Langan


Nov 20 '05 #4
Please post the solution you came up with.

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
never mind, I got it. Thanks
--
Strah @ Langan

"scorpion53061" <sc****************************@yahoo.com> wrote in message news:O5**************@TK2MSFTNGP10.phx.gbl...
Can I see the code you are using?

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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,
--
Strah @ Langan



Nov 20 '05 #5


DataTable.AcceptChanges() does the job. It forces grid to recognize changes
and accepts it as well.
Thanks,
--
Strah @ Langan

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
never mind, I got it. Thanks
--
Strah @ Langan

"scorpion53061" <sc****************************@yahoo.com> wrote in message news:O5**************@TK2MSFTNGP10.phx.gbl...
Can I see the code you are using?

"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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,
--
Strah @ Langan



Nov 20 '05 #6
Actually,

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

Thahnks

Strah

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7
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
"Strahimir Antoljak" <st***@netzero.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Actually,

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

Thahnks

Strah

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #8
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Glenn Owens | last post: by
2 posts views Thread by Manish | last post: by
3 posts views Thread by Jim Heavey | last post: by
3 posts views Thread by D. Shane Fowlkes | last post: by
5 posts views Thread by junglist | last post: by
5 posts views Thread by Tina | last post: by
1 post views Thread by Sharon | last post: by
9 posts views Thread by rn5a | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.