46

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.

jszigeti
  • 373
  • 4
  • 11
malcoauri
  • 11,904
  • 28
  • 82
  • 137

8 Answers8

73

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://learn.microsoft.com/dotnet/api/system.data.datarowcollection.add?view=net-5.0#System_Data_DataRowCollection_Add_System_Data_DataRow_

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
32

I found dotnetperls examples on DataRow very helpful. Code snippet for new DataTable from there:

static DataTable GetTable()
{
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("Weight", typeof(int));
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Breed", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
    table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
    table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
    table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
    table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);

    return table;
}
mimo
  • 6,221
  • 7
  • 42
  • 50
10

//َCreating a new row with the structure of the table:

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

//Giving values to the columns of the row(this row is supposed to have 28 columns):

for (int i = 0; i < 28; i++)
{
    row[i] = i.ToString();
}
Mehran Sarrafi
  • 131
  • 1
  • 3
3

You have to add the row explicitly to the table

table.Rows.Add(row);
rene
  • 41,474
  • 78
  • 114
  • 152
2

If need to copy from another table then need to copy structure first:

DataTable copyDt = existentDt.Clone();
copyDt.ImportRow(existentDt.Rows[0]);
Aleksey Kontsevich
  • 4,671
  • 4
  • 46
  • 101
1

This works for me:

var table = new DataTable();
table.Rows.Add();
x-silencer
  • 13
  • 1
0

try table.Rows.add(row); after your for statement.

Riccardo
  • 1,490
  • 2
  • 12
  • 22
-2
    GRV.DataSource = Class1.DataTable;
            GRV.DataBind();

Class1.GRV.Rows[e.RowIndex].Delete();
        GRV.DataSource = Class1.DataTable;
        GRV.DataBind();
  • 5
    Please try to avoid just dumping code as an answer and try to explain what it does and why. Your code might not be obvious for people who do not have the relevant coding experience. Please edit your answer to include [clarification, context and try to mention any limitations, assumptions or simplifications in your answer.](https://stackoverflow.com/help/how-to-answer) – Sᴀᴍ Onᴇᴌᴀ Jun 20 '17 at 19:23
  • 1
    This code is incomplete, and has nothing obvious to do with data rows. – jpaugh Apr 25 '19 at 17:52