When using a DataTable as a DataSource to a GridView in C#, it is often necessary to add additional rows, whether they be blank rows or fully populated rows, to the control. It has been my experience that it is much easier to add a DataRow into the DataTable prior to a DataBind.

The first step is to create an empty datatable. It would be perfectly legal to start with a datatable prepopulated with data. In this case, we are naming the Datatable, dt.

DataTable dt = new DataTable();

The next step is to create a new DataRow object. In this case, we are calling it “newRow”. The next step would be to initialize the DataRow object, which is the second line below:

DataRow myNewRow;
myNewRow = dt.NewRow();

Now that the DataRow has been created and initialized, simply add values to the object as you would an array. In the two examples below, we are adding values to two different columns in our row. We are first assigning the column with name customerId the integer value of 1. Secondly, we are assigning the column with name username a string johndoe. Repeat as needed.

myNewRow["customerId"] = 1;
myNewRow["username"] = “johndoe” ;

Finally, we want to add this newly created row (with two columns) to the originally blank datatable we created in the beginning.

dt.Rows.Add(myNewRow);

All the steps from above are put together in the example below:

DataTable dt = new DataTable();
DataRow myNewRow;
myNewRow = dt.NewRow();
myNewRow["customerId"] = 1;
myNewRow["username"] = “johndoe” ;
dt.Rows.Add(myNewRow);

With that you can now set this datatable as a DataSource and DataBind a GridView or any other similar C# control.

This entry was posted on Wednesday, June 18th, 2008 at 1:20 pm and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses

  1. Jignasha says

    Hi , this is very good article. But i want to add multiple rows to the particular column of datatable. I want to add array of rows to the datatable which is already existing

  2. Victor Chen says

    If you want to add multiple rows to a single particular column, that sounds like the equivalent to adding multiple rows, but only putting data in a single column (and all other columns are empty strings).

    If your DataTable already exists, all you need to do is ignore “DataTable dt = new DataTable();” line. Simply start with creating a Data Row. Replace all instances of dt with the name of your DataTable.

  3. Piotrek says

    I have similar problem as this one - I am trying to add a record to the table in Access database using vc# 2008 express edition. I also have table.AcceptChanges() method on the end but it does not do anything, no error, no data updated. Could somebody send a reply to my e-mail Piotr_Jonczyk@Yahoo.com?

  4. VISHAL says

    Hello,
    It is nice to see your example but, i want to add multiple records in datatable without write multiple time code…
    your code is good but i want to insert multiple records into to the same column. if i have n number of row then i have to write code n number of time so plzz…. anyone tell me how to add row into the datatable.
    i was used a grid but in i have show 1 to 10 serial number and in that perticular 1 No. he has to write something in textbox and i have to add it grid… it’s ok but i want to also add grid inside the grid for that i have a code either i can bind then i cannot show textbox record into grid at run time if i used add record in run time then i cannot add templete for grid anyone please reply ………
    my email is onlyvish4me@gmail.com

Leave a Reply