Home Forums WinForms controls Xceed Grid for WinForms Updating data to data source automatically. Reply To: Updating data to data source automatically.

User (Old forums)
Member
Post count: 23064

Hi

I have a solution that works with all the existing rows.
I use this myself.

I also use a IList that implements IBindingList, ITypedList, ISupportInitialize.

You need to subscribe to 3 events:

– cell.ValueChanging
– cell.ValidationError
– row.ValidationError

In initGrid() I bind the grid to the datasource and subscribe to the events.
cell_ValidationError(…) and row_ValidationError(…) are called by the grid, when a user enters a incorrect value.

The most interested stuff is in cell_ValueChanging(…) event.
This event is called before the data is commited, so it is possible to handle errors.

———————— code —————————————

//Cell validation error event
private void cell_ValidationError(object sender, Xceed.Grid.CellValidationErrorEventArgs e)
{
e.CancelEdit = false;
MessageBox.Show(e.ToString());
}

//Row validation error event
private void row_ValidationError(object sender, Xceed.Grid.RowValidationErrorEventArgs e)
{
e.CancelEdit = false;
MessageBox.Show(e.ToString());
}

//Cell value changing event
private void cell_ValueChanging(object sender, Xceed.Grid.ValueChangingEventArgs e)
{

//old cell value
object oldCellValue = ((Cell)(((DataCell)(sender)))).Value;

//field name
string changedPropertyName = ((Cell)(((DataCell)(sender)))).FieldName.ToString();

//index in collection
int collectionIndex = ((DataRow)((DataCell)(sender)).ParentRow).Index;

//changed object
ComposerEditable changedEntity = (ComposerEditable)this.list[collectionIndex];

//invoke the changed object with the new cell value
Type type = typeof(Composer);
type.InvokeMember(changedPropertyName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, changedEntity, new Object[] {e.NewValue});

try
{
//update database…
}
catch(Exception ex)
{
//update failed.
e.NewValue = oldCellValue;
e.Cancel = false;
MessageBox.Show(ex.Message);
}
}

private void initGrid()
{
this.gridControl1.BeginInit();

//test data
for(int i=0; i<10; i++)
{
Xceed.Grid.Samples.ComposerEditable composer = new ComposerEditable();
list.Add(composer);
}

//bind to datasource
this.gridControl1.DataSource = list;

//subscribe to events
foreach( Xceed.Grid.Cell cell in this.gridControl1.DataRowTemplate.Cells )
{
cell.ValueChanging += new Xceed.Grid.ValueChangingEventHandler(cell_ValueChanging);
cell.ValidationError += new Xceed.Grid.CellValidationErrorEventHandler(cell_ValidationError);
}
foreach( Xceed.Grid.DataRow row in gridControl1.DataRows)
{
row.ValidationError += new Xceed.Grid.RowValidationErrorEventHandler(row_ValidationError);
}
this.gridControl1.AddingDataRow += new AddingDataRowEventHandler(gridControl1_AddingDataRow);

this.gridControl1.EndInit();
}

Regards / venlig hilsen 🙂

/Thomas

Imported from legacy forums. Posted by C# (had 991 views)