Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Update Problem

Status
Not open for further replies.

MDTekUser

Technical User
Dec 25, 2005
68
US
Forgive the elementary nature of this question.

I am trying to update a datagrid. I'm just using the standard data adapter wizard to generate the commands for updating records.

I am able to view, cancel or delete records but I am not able to update. For some reason I am not able to get the updated values from the text box, even if I change the data it reverts back to the old value.

And as I stepped through the code, I realized the values weren't changing.

Here is an example :

Code:
private void dgd_UpdateCommand(object source, DataGridCommandEventArgs e)
{

   TextBox txt = (TextBox)e.Item.Cells[3].Controls[0];
   //Even though I am changing the data here it doesn't get represented in the text box, I'm still showing the same value as before
   lblMessage.Text = txt.Text;

   //Do some updating

}

I have view state turned on for both the page and the data grid. VS #2003
 
I didn't list the rest of the code because if the data in the text box is not changing then your update isn't going to work. But it consisted of creating a new datarow and then setting the values of datarow columns to its associated datagrid cell.
 
I see what I did wrong. I was calling the datagrid's databind method on page postback in page_load. I should have called it on !IsPostBack. I read a few articles that pointed out my mistake.

 
What I really find confusing is that there is really a big disparity in approaches to using the datagrid.

I think Scott Mitchell, one of the 4 guys, espouses the philosophy that you should mostly use data readers as datagrid datasources, and then update with a command object.

I like his approach because he showed that datareaders offer a higher performance than datasets.

 
Yes...fill the grid and use your grid commands to do updates, inserts, deletes etc. In your html for the grid you can specify your commands. OnUpdateCommand OnInsert. etc.

But remember that datareaders only go forward. Depending what you want to do with your grid, you may need to use Datasets. again, depending on how you need the grid to act.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top