We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

how to get the data row when adding new row by CurrentCellKeyDown event

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??


5 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team January 31, 2023 03:16 PM UTC

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;

           

}



Attachment: Samplerelationsfdatagrid_d82bcd4.zip


MO Mohamad January 31, 2023 06:12 PM UTC

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.



SJ Sathiyathanam Jeyakumar Syncfusion Team February 3, 2023 04:26 AM UTC

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";

    }

}



Attachment: Samplerelationsfdatagrid_9739b7aa.zip


MO Mohamad February 4, 2023 06:22 PM UTC

thanks,that's answer what I needed



SJ Sathiyathanam Jeyakumar Syncfusion Team February 6, 2023 09:58 AM UTC

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. 


Loader.
Up arrow icon