Adding an item to the datagrid

  • Thread starter Thread starter Chris Leuty
  • Start date Start date
C

Chris Leuty

I have a datagrid (with viewstate enabled), and my edit/update/delete
buttons in the grid are all working fine. I have a textbox and a button
underneath the grid for adding items to the grid. When the button is
clicked, the following code runs:

Dim drNew As DataRow = dsTrades.Tables("Trades").NewRow ' create
new row
dsTrades.Tables("Trades").Rows.Add(drNew)
' add new row to table
drNew("TradeName") = Request.Form("txtNewTrade") '
get TradeName from post
daTrades.Update(dsTrades)
' update database
daTrades.Fill(dsTrades)
' re-load the sorted list...
dgTrades.DataBind()
' bind the grid

The item adds properly to the database, but the newly added item always
appears as the first row in the grid, if the grid is currently on the first
page, regardless of where it should appear (it is a sorted list). Calling
the grid page again fixes the problem (i.e. the item begins to show up in
the correct place). My basic question is: why doesn't the Fill() and
DataBind() when I am adding refresh the grid before rendering?

Thanks,

Chris
 
Chris Leuty said:
I have a datagrid (with viewstate enabled), and my edit/update/delete
buttons in the grid are all working fine. I have a textbox and a button
underneath the grid for adding items to the grid. When the button is
clicked, the following code runs:

Dim drNew As DataRow = dsTrades.Tables("Trades").NewRow ' create
new row
dsTrades.Tables("Trades").Rows.Add(drNew)
' add new row to table
drNew("TradeName") = Request.Form("txtNewTrade") '
get TradeName from post
daTrades.Update(dsTrades)
' update database
daTrades.Fill(dsTrades)
' re-load the sorted list...
dgTrades.DataBind()

Why would this be sorted?
 
Technical Why: The proc returning the data is sorting the data
alphabetically. (It is a list of trade skills).
Business Why: That is how they want to see the data.
 
I cleared the dataset before filling it, and the grid refreshed in the
correct order.

All fixed.
 
Back
Top