Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new DataRow into DataTable?

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.

like image 284
malcoauri Avatar asked Sep 28 '12 14:09

malcoauri


People also ask

How do I create a new DataRow?

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.

How do I add a row to a DataTable in R?

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.

What is DataRow C#?

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.


1 Answers

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_

like image 190
Aghilas Yakoub Avatar answered Oct 02 '22 19:10

Aghilas Yakoub