how to give default values for a specify column with datagrid that has datatable as datasource

hi

I'm trying to give default values to specified columns while adding new row in sfDatagrid but my sfDatagrid has "DataTable" as datasource so, how to give that default values that I need to some columns that I need.

I found article : https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#initializing-default-values-for-addnewrow


but that example doesn't datatable so please if you can give me a solution for that I will be appreciated.



1 Reply

DM Dhanasekar Mohanraj Syncfusion Team July 25, 2022 01:58 PM UTC

Hi Mohamad,

You can achieve your requirement to set the default values for AddNewRow while initiating, through AddNewRowInitiatingEventArgs.NewObject property in AddNewRowInitiating event for the DataTable bound SfDataGrid is shown below.

this.sfDataGrid1.AddNewRowInitiating += SfDataGrid1_AddNewRowInitiating;

private void SfDataGrid1_AddNewRowInitiating(object sender, Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs e)

{

    var record = e.NewObject as System.Data.DataRow;

    record[0] = 101;                         // Setting default for EmplyeeID column 

    record[1] = "XXX";                       // Setting default for EmployeeName column

    record[2] = "YYY";                       // Setting default for CustomerID column

    record[3] = "UUU";                       // Setting default for Country column

    record[4] = new DateTime(2019, 10, 15);  // Setting default for Date column

}


Please f
ind the sample in the attachment and let us know if you have any concerns about this.

Regards,
Dhanasekar M.


Attachment: SfDataGridDemo_238646cf.zip

Loader.
Up arrow icon