hi
I'm using (CurrentCellKeyDown) event to make some actions when adding a new row to the SfDatagrid but I have a problem, how do I get the current row value for all cells (here I'm talking when I'm trying to add new row from the SfDatagrid)
I mean I need to get all values to all cells in that row in (CurrentCellKeyDown) event??
Hi Mohamd,
The AddNewRowInitiating event is triggered when the add new cell enters edit mode And its allowing you to retrieve the new row's data object by using its event args. To access the data object, simply handle the event. Refer to the below code snippets to get more information.
this.sfDataGrid1.AddNewRowInitiating += SfDataGrid1_AddNewRowInitiating;
private void SfDataGrid1_AddNewRowInitiating(object sender, Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs e) { //You can access the new adding row object here. orderInfo = e.NewObject as OrderInfo;
} |
If you want to enter edit mode when type the letter directly on the cell you should call the BeginEdit() method to going to edit mode. Once in an edit mode, you can retrieve the new row's data object in the AddNewRowInitiating event. You can also use the same object in the CurrentCellKeyDown event.
this.sfDataGrid1.TableControl.KeyDown += TableControl_KeyDown; this.sfDataGrid1.CurrentCellKeyDown += SfDataGrid1_CurrentCellKeyDown; this.sfDataGrid1.AddNewRowInitiating += SfDataGrid1_AddNewRowInitiating;
private void TableControl_KeyDown(object sender, KeyEventArgs e) { //We should call the beginEdit if we directly type the keys in AddNewRowCell if(this.sfDataGrid1.CurrentCell != null && this.sfDataGrid1.CurrentCell.RowIndex == this.sfDataGrid1.GetAddNewRowIndex()) this.sfDataGrid1.CurrentCell.BeginEdit(); }
OrderInfo orderInfo = null;
private void SfDataGrid1_CurrentCellKeyDown(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs e) { if(orderInfo != null) { // You can make the customization here. } }
private void SfDataGrid1_AddNewRowInitiating(object sender, Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs e) { //You can access the new adding row object here. orderInfo = e.NewObject as OrderInfo;
} |
actually, that answer is not appropriate for my case, what I need is how to get access to the new row values in the (CurrentCellKeyDown) event.
by the way, I use Datatable as a Datasource
please see the following image:
you can see in the (1st) line that I can get the DataRow from (e.DataRow.RowData) and in the (2nd) I can get the cell value as you see, so in the ( CurrentCellKeyDown ) I can't do that so I need the similar method to do that.
Mohamad,
To add a new row in SfDataGrid by using the AddNewRow, the new object will only be initialized when the add new row cell goes into edit mode. When you directly type the letters in AddNewRowCell, then you need to call the BeginEdit method to start editing the add new row cell . Once the cell is in edit mode, you can access the newly added object using the SfDataGrid.View.CurrentAddItem property. Please refer to the code snippets below for an example.
private void SfDataGrid1_CurrentCellKeyDown(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs e) { if (this.sfDataGrid1.CurrentCell != null && !this.sfDataGrid1.CurrentCell.IsEditing && this.sfDataGrid1.CurrentCell.RowIndex == this.sfDataGrid1.GetAddNewRowIndex()) this.sfDataGrid1.CurrentCell.BeginEdit(); var mappingName = this.sfDataGrid1.CurrentCell.Column.MappingName; var datarow = this.sfDataGrid1.View.CurrentAddItem as System.Data.DataRow; if (mappingName == "Col1") { // You can make the customization here. datarow["Col2"] = "Syncfusion"; } } |
thanks,that's answer what I needed
Mohamad,
We are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.