RowState remain unchanged after edit

  • Thread starter Thread starter Colin Mackie
  • Start date Start date
C

Colin Mackie

Appreciate any help with this one.

I have a DataRow loaded up from the database and the DataTable is bound to a
DataGrid.

However, I want to be able to change the data programmatically and only use
the DataGrid to display the current data. When a user double clicks a row, a
dialog show and allows them to make changes and I am using
"row.item(fieldname) = value" to set the data up. The row is changed,
DataAdapter saves it, and the grid updates. Great.

Now if I sort the grid on a column, so it's 'out of normal order', when the
"row.item(fieldname) = value" is called, the RowState remains as
Unchanged...even though I can see that the data has changed in its internal
array! So the DataAdapter doesn't write it away, and everything gets screwed
from there because the DataRow doesn't match the database.

I have had a look around and found some references to something similar and
suggesting calling row.EndEdit() or CurrencyManager.EndCurrentEdit(), but
this has no effect.

Any ideas???

Cheers,


Colin
 
If you get a reference to the wrong row after sorting the DataGrid; it
sounds like a CurrencyManager problem.

Here is a demo app that shows the currently selected data using the method
you are using, and by using the CurrencyManager. When unsorted both methods
work, when sorted only the CurrencyManager method works...

==========================================================================
private void updateInfo()
{
int index;
int totrows;
index = dgCustomers.CurrentRowIndex;
//show info based on DataGrid properties:
lblCurrentRowIndex1.Text = "CurrentRowIndex = " + index.ToString();
totrows = ds.Tables["Customers"].Rows.Count;
lblDataTableCount.Text = "DataRows = " + totrows.ToString();
lblCustomerID1.Text = "CustomerID = " +
ds.Tables["Customers"].Rows[index]["CustomerID"].ToString();

//show info based on CurrencyManager:
CurrencyManager cm;
cm = (CurrencyManager)dgCustomers.BindingContext[dgCustomers.DataSource,
dgCustomers.DataMember];
lblCurrentRowIndex2.Text = "CurrentRowIndex = " + index.ToString();
//get the # of rows currently displayed in the grid:
totrows = cm.Count;
lblCurrencyManagerCount.Text = "CurrencyManager Count = " +
totrows.ToString();
//get the DataRow referenced by the current row in the grid:
DataRow dr = ((DataRowView)cm.Current).Row;
lblCustomerID2.Text = "CustomerID = " + dr["CustomerID"].ToString();
}
==========================================================================

The full source code showing other DataGrid tricks can be found at:
http://www.zrgwortz.com/datagrids.zip

(I am not the author of this code, nor am I responsible for hosting this
file. There were no viruses or malicious code at the download site last
time I downloaded it, but I can not be held responsible for the destination
of the link. I was given this link at the July 2003 meeting of the St Louis
CSharp.NET user group as part of the presentation.)

You can also find other useful winforms binding code at:

(George Shepherd's Windows Forms FAQ)
http://www.syncfusion.com/FAQ/WinForms/

Michael Lang, MCSD
 
Back
Top