Datagrid Cell validation

  • Thread starter Thread starter Nancy
  • Start date Start date
N

Nancy

By using the datagrid.currentcellchanged, I can get a
grid row/column value before changes.
How could I validate if this row/column has been modified?
Thanks.
Nancy.
 
If you are using a DataTable to bind to the DataGrid, then you can hook into
a few events on the DataTable that should notify you when a change has been
made (or is in the process of being made):
ColumnChanging, ColumnChanged, RowChanging, RowChanged
 
Hi Nancy,

Thanks for your post!

From my understanding, you want to validate the cell value when you are
moving to another grid cell.

Here is an simple sample to do this, please reply to this thread if you
still have problem on it.
Thanks !
<code>
//
private DataGridCell oldCell;
private bool cellError = false;
...
//In OnLoad event handler
ds.Tables[0].ColumnChanging += new
DataColumnChangeEventHandler(columnChanging);
grid.CurrentCellChanged += new EventHandler(cellChanged);
...

void columnChanging(object o, DataColumnChangeEventArgs e)
{
oldCell = grid.CurrentCell;
//do check
// don't allow "Test" in ContactName field.
cellError = (e.Column.ColumnName == "ContactName" &&
e.ProposedValue.ToString().ToUpper() == "TEST");
}

void cellChanged(object o, EventArgs e)
{
if (cellError)
grid.CurrentCell = oldCell;
}
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Back
Top