I have a DataGridView
binded to a DataTable
(DataTable
binded to database). I need to add a DataRow
to the DataTable
. I'm trying to use the following code:
dataGridViewPersons.BindingContext[table].EndCurrentEdit(); DataRow row = table.NewRow(); for (int i = 0; i < 28; i++) { row[i] = i.ToString(); }
But it doesn't work, DataGridView
has never been added a new row. Please, tell me, how can I fix my code?
Thank you in advance.
To create a new DataRow, use the NewRow method of the DataTable object. After creating a new DataRow, use the Add method to add the new DataRow to the DataRowCollection. Finally, call the AcceptChanges method of the DataTable object to confirm the addition.
With command rbindlist from the data. table package, we can append dt_add_row and new_row row-wise. Object dt_add_row, shown in Table 2, shows the original data. table with the added row number 6.
A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table.
You can try with this code - based on Rows.Add method
DataTable table = new DataTable(); DataRow row = table.NewRow(); table.Rows.Add(row);
Reference: https://docs.microsoft.com/dotnet/api/system.data.datarowcollection.add?view=net-5.0#System_Data_DataRowCollection_Add_System_Data_DataRow_
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With