views:

40

answers:

1

I have a datagrid view which shows table data from a database

Environment: - C# 4.0 - SQL Server 2005 - Visual studio 2010

What i want is: On a row to row basis automatically save changes. So when I edit the first row and then leave the row. It should save the changes to the database.

I've used this code:

In the From1 Load:

dataSet1.TblObject.RowChanged += rowUpdate;

The eventhandler:

private void rowUpdate(object sender, DataRowChangeEventArgs e)
{ 
    DataRow dr = e.Row;
    if (dr.RowState == DataRowState.Modified)
    {
        tblObjectTableAdapter.Update(dr);
    }
}

When I've edited a row in the grid, tblObjectTableAdapter.Update(dr); is called the data is stored to the table. Which is good. But the event keeps on triggering as if it's called recursively. So the tblObjectTableAdapter.Update(dr); is causing a dataSet1.TblObject.RowChanged event. But I can't differentiate between the both of them. I'm guessing I'm using the wrong event to store my values but I can't find any event that sounds logical to me. What am I doing wrong?

I've made a dataset xsd:

TblObject

I've picked the dataset as datasource in this gridview:

alt text

A: 

Ok, I read your post a couple of times now, while I can't help u directly with your problem. I'll try anyway, What u want is that when i make a change, it doesn't update itself immediatly, but saves the information in a list and when u press a button save the updates?

Then You should make a List of Datarows, and each time a row is changed add that item to the list, this way the updates aren't made directly, but only when u press the button. (This is also way better for performance sake.)

So instead of the code

DataRow dr = e.Row; 
if (dr.RowState == DataRowState.Modified) 
{ 
    tblObjectTableAdapter.Update(dr); 
}

U should make

 DataRow dr = e.Row; 
    if (dr.RowState == DataRowState.Modified) 
    { 
DatarowLst.Add(dr); 
    } 

ButtonSave_Click(object sender, eventargs e)
{
foreach (DataRow d in DatarowLst)
{
tblObjectTableAdapter.Update(d); 
}
}

EDIT:

Ok sorry for misunderstanding u. I now understand your problem and looked into the issue.

I found some interesting information on Data adapters and tables. It seems the Adapter does a row to row check in order to obtain information about the changes made.(i.e does it need a update or delete command?) Thereby, My GUESS is that this triggers a update to occur and therefore call your event again, and running the method recursivly.

Info found at the REMARK piece on: This site from msdn

They give a link to this site for more information. Maybe you'll find your answer there.

As to a solution for your problem it seems u can leave your event out and just call the update command. Im NOT sure about this cause I dont have time to check on this and I have little experience on this subject.

Well, hope this helps u a bit further..

Emerion
No sorry that's not what I'm trying to do. I've edited my question maybe its more clear now.
Sjuul Janssen