Datagrid: click edit and all the rows disappear!

T

trebor

Most of this code comes from or follows the walkthrough (Using a
DataGrid Web Control to Read and Write Data). The problem is that as
soon as you click the Edit link, the data rows disappear, leaving you
with just the header row. I've figured out what's happening, but can't
figure out why.

Following along with the debugger, as soon as you click Edit, execution
goes to Page_Load, and at that point, before it even checks postback,
the dataset is empty. All the rows are gone. Anyone know why?

I suppose I could refill the dataset in Page_Load, but that leads to the
dreaded "Old Values" problem.

This doesn't make sense! Why would the bottom fall out of the dataset?
The structure is still there, all the column names and everything, but
the data itself is gone -- rows.count returns 0.

Thanks for any help!

Private Sub Page_Load(ByVal sender As System.Object, ByVal e ...
If Not Page.IsPostBack Then
OleDbDataAdapter1.Fill(myDataSet1)
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e ...
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e...
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e...
Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
Dim strA, strB, strC As String
Dim txt As TextBox
txt = CType(e.Item.Cells(1).Controls(0), TextBox)
strA = txt.Text
txt = CType(e.Item.Cells(2).Controls(0), TextBox)
strB = txt.Text
txt = CType(e.Item.Cells(3).Controls(0), TextBox)
strC = txt.Text

Dim r As myDataSet.XRow
r = myDataSet1.X.FindByRecID(key)
r.A = strA
r.B = strB
r.C = strC
OleDbDataAdapter1.Update(myDataSet1)
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
End Class
 
S

Scott Allen

Typically you only fill the DataSet once - the first Page_Load event.
After that the DataGrid will keep around the data it needs to
redisplay in the ViewState. Are you checking for rows in the DataSet
or data in the DataGrid on postback?
 
T

trebor

Scott Allen said:
Typically you only fill the DataSet once - the first Page_Load event.
After that the DataGrid will keep around the data it needs to
redisplay in the ViewState.

Do you know if it does this automatically? Or is there some coding to do
to accomplish this?
Are you checking for rows in the DataSet or data in the DataGrid on
postback?

Well, the data is gone from both places. It's the same data, isn't it?
Or does the DataGrid actually make a copy?
 
S

Scott Allen

Do you know if it does this automatically? Or is there some coding to do
to accomplish this?

This happens automatically unless you have EnableViewState = false
somwhere in the code or in the ASPX.
Well, the data is gone from both places. It's the same data, isn't it?
Or does the DataGrid actually make a copy?

The DataSet object will be empty for certain in Page_Load, but the
DataGrid should be reloaded with values kept in the ViewState on the
client.

You might want to look through the following article and compare code
to see what is different:

Walkthrough: Using a DataGrid Web Control to Read and Write Data
http://msdn.microsoft.com/library/d...ughusingdatagridwebcontroltoreadwritedata.asp

Hope this helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top