How to add new DataRow into DataTable?

malcoauri picture malcoauri · Sep 28, 2012 · Viewed 209.4k times · Source

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.

Answer

Aghilas Yakoub picture Aghilas Yakoub · Sep 28, 2012

You can try with this code - based on Rows.Add method

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);

Link : https://msdn.microsoft.com/en-us/library/9yfsd47w.aspx