249

I've tried creating a DataTable and adding rows to it like this:

 DataTable dt = new DataTable();
 dt.clear();
 dt.Columns.Add("Name");
 dt.Columns.Add("Marks");

How do I see the structure of DataTable?

Now I want to add ravi for Name and 500 for Marks. How can I do this?

jordanz
  • 367
  • 4
  • 12
Cute
  • 13,643
  • 36
  • 96
  • 112
  • 2
    @Cute. Just a comment- if you are using .NET 3.5 you should really see what LINQ to SQL offers. If you do decide to go down the DataTable/DataSet route at least look in to creating DataTables that are strongly typed. – RichardOD Jun 25 '09 at 07:50

14 Answers14

318

Here's the code:

DataTable dt = new DataTable(); 
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
DataRow _ravi = dt.NewRow();
_ravi["Name"] = "ravi";
_ravi["Marks"] = "500";
dt.Rows.Add(_ravi);

To see the structure, or rather I'd rephrase it as schema, you can export it to an XML file by doing the following.

To export only the schema/structure, do:

dt.WriteXMLSchema("dtSchemaOrStructure.xml");

Additionally, you can also export your data:

dt.WriteXML("dtDataxml");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
83

You can also pass in an object array as well, like so:

DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
object[] o = { "Ravi", 500 };
dt.Rows.Add(o);

Or even:

dt.Rows.Add(new object[] { "Ravi", 500 });
James McConnell
  • 2,180
  • 3
  • 19
  • 29
  • 12
    Another alternative on this is to note the `Add` method shown above has an overload with variable number of params, so you could shorten this to `dt.Rows.Add("Ravi", 500);` and would work the same. One big warning with either of these approaches: you must supply these parameters in exact same order as the columns were defined, else you'll get an error. (So use with caution!) – Funka Jan 29 '13 at 19:41
82

Create DataTable:

DataTable MyTable = new DataTable(); // 1
DataTable MyTableByName = new DataTable("MyTableName"); // 2

Add column to table:

 MyTable.Columns.Add("Id", typeof(int));
 MyTable.Columns.Add("Name", typeof(string));

Add row to DataTable method 1:

DataRow row = MyTable.NewRow();
row["Id"] = 1;
row["Name"] = "John";
MyTable.Rows.Add(row);

Add row to DataTable method 2:

MyTable.Rows.Add(2, "Ivan");

Add row to DataTable method 3 (Add row from another table by same structure):

MyTable.ImportRow(MyTableByName.Rows[0]);

Add row to DataTable method 4 (Add row from another table):

MyTable.Rows.Add(MyTable2.Rows[0]["Id"], MyTable2.Rows[0]["Name"]);

Add row to DataTable method 5 (Insert row at an index):

MyTable.Rows.InsertAt(row, 8);
Hamed Naeemaei
  • 8,052
  • 3
  • 37
  • 46
38
// Create a DataTable and add two Columns to it
DataTable dt=new DataTable();
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Age",typeof(int));

// Create a DataRow, add Name and Age data, and add to the DataTable
DataRow dr=dt.NewRow();
dr["Name"]="Mohammad"; // or dr[0]="Mohammad";
dr["Age"]=24; // or dr[1]=24;
dt.Rows.Add(dr);

// Create another DataRow, add Name and Age data, and add to the DataTable
dr=dt.NewRow();
dr["Name"]="Shahnawaz"; // or dr[0]="Shahnawaz";
dr["Age"]=24; // or dr[1]=24;
dt.Rows.Add(dr);

// DataBind to your UI control, if necessary (a GridView, in this example)
GridView1.DataSource=dt;
GridView1.DataBind();
Christopher Berman
  • 719
  • 1
  • 5
  • 21
shahnawaz
  • 457
  • 5
  • 3
32

To add a row:

DataRow row = dt.NewRow();
row["Name"] = "Ravi";
row["Marks"] = 500;
dt.Rows.Add(row);

To see the structure:

Table.Columns
djdd87
  • 67,346
  • 27
  • 156
  • 195
25

You can add Row in a single line

    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
Diego
  • 2,238
  • 4
  • 31
  • 68
21

You can write one liner using DataRow.Add(params object[] values) instead of four lines.

dt.Rows.Add("Ravi", "500");

As you create new DataTable object, there seems no need to Clear DataTable in very next statement. You can also use DataTable.Columns.AddRange to add columns with on statement. Complete code would be.

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name"), new DataColumn("Marks") });     
dt.Rows.Add("Ravi", "500");
Adil
  • 146,340
  • 25
  • 209
  • 204
13

The easiest way is to create a DtaTable as of now

DataTable table = new DataTable
{
    Columns = {
        "Name", // typeof(string) is implied
        {"Marks", typeof(int)}
    },
    TableName = "MarksTable" //optional
};
table.Rows.Add("ravi", 500);
naveen
  • 53,448
  • 46
  • 161
  • 251
11
DataTable dt=new DataTable();
Datacolumn Name = new DataColumn("Name");
Name.DataType= typeoff(string);
Name.AllowDBNull=false; //set as null or not the default is true i.e null
Name.MaxLength=20; //sets the length the default is -1 which is max(no limit)
dt.Columns.Add(Name);
Datacolumn Age = new DataColumn("Age", typeoff(int));`

dt.Columns.Add(Age);

DataRow dr=dt.NewRow();

dr["Name"]="Mohammad Adem"; // or dr[0]="Mohammad Adem";
dr["Age"]=33; // or dr[1]=33;
dt.add.rows(dr);
dr=dt.NewRow();

dr["Name"]="Zahara"; // or dr[0]="Zahara";
dr["Age"]=22; // or dr[1]=22;
dt.rows.add(dr);
Gv.DataSource=dt;
Gv.DataBind();
James Ferretti
  • 171
  • 2
  • 15
9
DataTable dt=new DataTable();
DataColumn Name = new DataColumn("Name",typeof(string)); 

dt.Columns.Add(Name);
DataColumn Age = new DataColumn("Age", typeof(int));`

dt.Columns.Add(Age);

DataRow dr=dt.NewRow();

dr["Name"]="Kavitha Reddy"; 
dr["Age"]=24; 
dt.add.Rows(dr);
dr=dt.NewRow();

dr["Name"]="Kiran Reddy";
dr["Age"]=23; 
dt.Rows.add(dr);
Gv.DataSource=dt;
Gv.DataBind();
kavitha Reddy
  • 3,303
  • 24
  • 14
8

You have to add datarows to your datatable for this.

// Creates a new DataRow with the same schema as the table.
DataRow dr = dt.NewRow();

// Fill the values
dr["Name"] = "Name";
dr["Marks"] = "Marks";

// Add the row to the rows collection
dt.Rows.Add ( dr );
rahul
  • 184,426
  • 49
  • 232
  • 263
4

In addition to the other answers.

If you control the structure of the DataTable there is a shortcut for adding rows:

// Assume you have a data table defined as in your example named dt dt.Rows.Add("Name", "Marks");

The DataRowCollection.Add() method has an overload that takes a param array of objects. This method lets you pass as many values as needed, but they must be in the same order as the columns are defined in the table.

So while this is a convenient way to add row data, it can be risky to use. If the table structure changes your code will fail.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
1

Question 1: How do create a DataTable in C#?

Answer 1:

DataTable dt = new DataTable(); // DataTable created

// Add columns in your DataTable
dt.Columns.Add("Name");
dt.Columns.Add("Marks");

Note: There is no need to Clear() the DataTable after creating it.

Question 2: How to add row(s)?

Answer 2: Add one row:

dt.Rows.Add("Ravi","500");

Add multiple rows: use ForEach loop

DataTable dt2 = (DataTable)Session["CartData"]; // This DataTable contains multiple records
foreach (DataRow dr in dt2.Rows)
{
    dt.Rows.Add(dr["Name"], dr["Marks"]);
}
0
  1. Create datatabe with 2 columns: Name & Marks

    IList columns = new List() {"Name", "Marks"};

    Datatabel dt = new Datatable();

    foreach (string column in columns)

    dtSalesOrder.Columns.Add(column, typeof(string));

  2. Add data to datatable

    dt.Rows.Add("Ravi","500");