Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataRowState not changing to modified state
 

DataRowState not changing to modified state

I'm trying out this article on codeproject for my DataGridView: Autosaving

http://www.codeproject.com/cs/datab...&select=2075150

But none of my rows are getting saved to the database when I change to a new row or close my form.
My problem seems to be in this code :

Code:

private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
if (LastDataRow.RowState == DataRowState.Modified)
{
tblMaintenanceWorkTableAdapter.Update(LastDataRow);
}
}
}


I never get to the update function because the RowState doesn't seem to be modified, although I fill in all the columns of my row.

I also checked the LastDataRow and it is filled with a row through this code :

Code:

private void tblMaintenanceWorkBindingSource_PositionChanged(object sender, EventArgs e)
{
// if the user moves to a new row, check if the
// last row was changed
BindingSource thisBindingSource = (BindingSource)sender;
DataRow ThisDataRow = ((DataRowView)thisBindingSource.Current).Row;

if (ThisDataRow == LastDataRow)
{
// we need to avoid to write a datarow to the
// database when it is still processed. Otherwise
// we get a problem with the event handling of
//the DataTable.
throw new ApplicationException("It seems the" +
" PositionChanged event was fired twice for" +
" the same row");
}

UpdateRowToDatabase();

// track the current row for next
// PositionChanged event
LastDataRow = ThisDataRow;
}


so what do you guys think I'm still doing wrong?
Did I oversee something in the article?
da_cobra  Saturday, December 01, 2007 9:30 AM

Hi da_cobra,

You should not call the AcceptChanges method before update the data source. Quote from the document:

“When invoking AcceptChanges, the EndEdit method is implicitly called to end any edits. If the RowState of the row was Added or Modified, the RowState becomes Unchanged. If the RowState was Deleted, the row is removed.�

I think you code will work well if you call AcceptChanges method after updating the data source.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Monday, December 10, 2007 1:54 AM
thx I finally got it working :

Code Block

private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
//MessageBox.Show(String.Format("{0}", LastDataRow.RowState));

switch (LastDataRow.RowState)
{
case DataRowState.Modified:
{
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
//MessageBox.Show("Logboek item succesvol gewijzigd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het wijzigen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Added:
{
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol toegevoegd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het toevoegen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Deleted:
{
try
{
//LastDataRow.Delete();
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol verwijderd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het verwijderen van het logboek item: \n" + ex.Message);
}

break;
}
}
}
}



updateCommand :

UPDATE [tblLogbook]

SET [LogDate] = @LogDate,
[LogInfo] = @LogInfo,
[LogPersons] = @LogPersons,
[LogSparePartsUsed] = @LogSparePartsUsed,
[LogInstallationID] = @LogInstallationID

WHERE (([LogInstallationID] = @Original_InstallationID))

I'm still not 100% got the delete command working yet.
da_cobra  Tuesday, December 25, 2007 9:54 AM
anyone, pls?


Sad
da_cobra  Tuesday, December 04, 2007 2:12 PM

Hi da_cobra,

Did you develop you application according to the following article on code project? I have tested the sample in this article and it worked well on my machine.

· http://www.codeproject.com/KB/database/DataGridView2Db.aspx

Since the RowState didn’t change to DataRowState.Modified, I would suggest you check if you have called AcceptChanges method or RejectChanges method somewhere in your code.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, December 06, 2007 12:37 PM
It was indeed that article.

I didn't call the AcceptChanges() method, because it wasn't in the article.
I changed my code to this now :

Code Block

private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
try
{
MessageBox.Show("Accept changes...");
LastDataRow.AcceptChanges();
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}

if (LastDataRow.RowState == DataRowState.Modified)
{
MessageBox.Show("Saving...");

tblLogbookTableAdapter.Update(LastDataRow);
}
}
}





The LastDataRow.AcceptChanges() method is called in my application, but still the LastDataRow.RowState doesn't change to DataRowState.Modified.

Where did you call the AcceptChanges() method?






edit : I changed my code to this :


Code Block

private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
try
{
MessageBox.Show("Accept changes...");
LastDataRow.AcceptChanges();

int RowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (RowsAffected > 0)
MessageBox.Show("Data Saved");
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}

if (LastDataRow.RowState == DataRowState.Modified)
{
MessageBox.Show("Saving...");

tblLogbookTableAdapter.Update(LastDataRow);
}
}
}


I get my messagebox that says : Accept changes, but still the data isn't saved through the Update() method of the tableadapter. (O rows affected) Sad

What am I doing wrong. Crying
da_cobra  Friday, December 07, 2007 4:59 PM

Hi da_cobra,

You should not call the AcceptChanges method before update the data source. Quote from the document:

“When invoking AcceptChanges, the EndEdit method is implicitly called to end any edits. If the RowState of the row was Added or Modified, the RowState becomes Unchanged. If the RowState was Deleted, the row is removed.�

I think you code will work well if you call AcceptChanges method after updating the data source.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Monday, December 10, 2007 1:54 AM
ok, I changed my code to this :

Code Block
private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
if (LastDataRow.RowState == DataRowState.Modified)
{
MessageBox.Show("Saving...");

tblLogbookTableAdapter.Update(LastDataRow);
LastDataRow.AcceptChanges();
}
}
}


But this leaves me again to my first problem : LastDataRow.RowState doesn't changes to DataRowState.Modified?Sad


edit : I think there is a misunderstandment : The code in that article : does it also save a new row?
da_cobra  Monday, December 10, 2007 5:31 PM

Hi da_cobra,

The RowState for a new added row is DataRowState.Added, if you want to update a new added row, try to replace your code with the following lines.

Code Block

private void UpdateRowToDatabase()

{

if (LastDataRow != null)

{

if (LastDataRow.RowState == DataRowState.Modified || LastDataRow.RowState == DataRowState.Added)

{

MessageBox.Show("Saving...");

tblLogbookTableAdapter.Update(LastDataRow);

LastDataRow.AcceptChanges();

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, December 11, 2007 1:25 AM
Thx alot for the tip!
Adding a new row seems to work now, I still have to test it throughly though.

But changing a row doesn't work.

When I change something in a row and then move to another row I get the following error :


"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

on this line :

tblLogbookTableAdapter.Update(LastDataRow);


But I know that means that I have to add a sql update command to my TableAdapter.
For the moment it doesn't seem to create one automatically through the Configure-command of the TableAdapter.

I'll keep you posted


Thx for the help so far!
da_cobra  Tuesday, December 11, 2007 4:26 PM
hmm, I added a new method Update() to my tableadapter, but I still get the error :

"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

on this line

tblLogbookTableAdapter.Update(LastDataRow);


Any idea what I'm still doing wrong?


private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
MessageBox.Show(String.Format("{0}", LastDataRow.RowState));

switch (LastDataRow.RowState)
{
case DataRowState.Modified:
{
MessageBox.Show("Modified...");
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol gewijzigd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het wijzigen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Added:
{
MessageBox.Show("Added...");
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol toegevoegd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het toevoegen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Deleted:
{
MessageBox.Show("Deleted...");
try
{
LastDataRow.Delete();
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol verwijderd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het verwijderen van het logboek item: \n" + ex.Message);
}

break;
}
}
}
}


Deleting a row also doesn't work yet, by the way Sad
da_cobra  Wednesday, December 12, 2007 6:15 PM
more advice is appreciated Smile
da_cobra  Wednesday, December 19, 2007 5:48 PM

Hi da_cobra,

I think it is because you didn’t create the UpdateCommand and DeleteCommand for your TableAdapter, or the UpdateCommand and DeleteCommand is not correct. Please try to re-create UpdateCommand and DeleteCommand.

More information, please check the following link
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder(VS.80).aspx

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, December 20, 2007 1:42 AM
thx I finally got it working :

Code Block

private void UpdateRowToDatabase()
{
if (LastDataRow != null)
{
//MessageBox.Show(String.Format("{0}", LastDataRow.RowState));

switch (LastDataRow.RowState)
{
case DataRowState.Modified:
{
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
//MessageBox.Show("Logboek item succesvol gewijzigd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het wijzigen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Added:
{
try
{
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol toegevoegd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het toevoegen van het logboek item: \n" + ex.Message);
}

break;
}

case DataRowState.Deleted:
{
try
{
//LastDataRow.Delete();
int rowsAffected = tblLogbookTableAdapter.Update(LastDataRow);
if (rowsAffected > 0)
{
MessageBox.Show("Logboek item succesvol verwijderd.");
}
}
catch (Exception ex)
{
MessageBox.Show("Probleem bij het verwijderen van het logboek item: \n" + ex.Message);
}

break;
}
}
}
}



updateCommand :

UPDATE [tblLogbook]

SET [LogDate] = @LogDate,
[LogInfo] = @LogInfo,
[LogPersons] = @LogPersons,
[LogSparePartsUsed] = @LogSparePartsUsed,
[LogInstallationID] = @LogInstallationID

WHERE (([LogInstallationID] = @Original_InstallationID))

I'm still not 100% got the delete command working yet.
da_cobra  Tuesday, December 25, 2007 9:54 AM

Does anyone know of situations when RowState modified is not set even when data is edited in the controls ? I have the same situaion. Whatever I do, the rowstate is not set to 'Modified'?

Thanks


___________________________________________________ Thanks anandnairv - Always learning something...
anandnairv  Saturday, August 08, 2009 1:10 AM

You can use google to search for other answers

Custom Search

More Threads

• Click events on datagrid
• Datasource, Datamember, Displaymemeber, Valuemember
• Binding checkboxes and DBNULL errors
• Data bound Form Dirty Flag
• DataGridViewComboBoxColumn Vb.net Win Appli
• Modifing The Order of The Column Heads When Binding An Object
• DataGrid printing tiny data
• combobox
• DataGridView row height when using AutoSizeRowsMode AllCells
• Resetting GridView