capturing the modifications in a datagrid
-
I want to check whether the content in a datagrid cell has been changed or not... how can u achieve this? samitha
The cell itself doesn't record anything - the underlying data set does. If you're binding to a
DataSet
,DataTable
, orDataView
, get theDataRow
for the row for that cell and query theDataRow.RowState
property. There's several ways to do this. The easiest that takes sorting into account is like so:CurrencyManager cm = (CurrencyManager)
dataGrid1.BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
if (cm != null)
{
DataView view = (DataView)cm.List;
DataRowView rowView = view.Item[dataGrid1.CurrentRowIndex];
bool changed = rowView.Row.RowState == DataRowState.Modified;
// Do something based on 'changed'
}Microsoft MVP, Visual C# My Articles
-
The cell itself doesn't record anything - the underlying data set does. If you're binding to a
DataSet
,DataTable
, orDataView
, get theDataRow
for the row for that cell and query theDataRow.RowState
property. There's several ways to do this. The easiest that takes sorting into account is like so:CurrencyManager cm = (CurrencyManager)
dataGrid1.BindingContext[dataGrid1.DataSource,
dataGrid1.DataMember];
if (cm != null)
{
DataView view = (DataView)cm.List;
DataRowView rowView = view.Item[dataGrid1.CurrentRowIndex];
bool changed = rowView.Row.RowState == DataRowState.Modified;
// Do something based on 'changed'
}Microsoft MVP, Visual C# My Articles
thnks 4 the reply...My actual requiremnt is this.. Suppiose u have a datagrid with only one row has been entred. When you click on the close button on the form(without giving the focus on any other control) it should check whether the row has a value. This ckeck happens correctly when u click else where on the form or on the next row and then try to close.. can anybody help me...? samitha
-
thnks 4 the reply...My actual requiremnt is this.. Suppiose u have a datagrid with only one row has been entred. When you click on the close button on the form(without giving the focus on any other control) it should check whether the row has a value. This ckeck happens correctly when u click else where on the form or on the next row and then try to close.. can anybody help me...? samitha
Please state your actual requirement next time. It will save time. You should still get the currency manager like I showed you in the first reply in your
Form.Closing
event handler. Then you can callCurrencyManager.EndCurrentEdit
. See the method documentation forCurrencyManager.EndCurrentEdit
for an example. You could also useDataGrid.EndEdit
, which also includes an example of usage in the method documentation in the .NET Framework SDK.Microsoft MVP, Visual C# My Articles