Coder Perfect

How to create a DataTable in C# and how to add rows?

Problem

In C#, how do you make a data table?

This pleased me:

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

What’s the best way to see the DataTable’s structure?

Now I’d like to add ravi to the Name field and 500 to the Marks field. I’m not sure how I’m going to accomplish it.

Asked by Cute

Solution #1

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);

You may export the structure, or as I like to call it, schema, to an XML file by doing the following.

If you merely want to export the schema/structure, use these instructions:

dt.WriteXMLSchema("dtSchemaOrStructure.xml");

You can also export your data as follows:

dt.WriteXML("dtDataxml");

Answered by this. __curious_geek

Solution #2

You can also pass an object array, such as this:

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 });

Answered by James McConnell

Solution #3

Create DataTable:

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

Table with a column:

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

Method 1: Insert a row into a DataTable

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

Method 2 for adding a row to a DataTable:

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

Method 3 (adding a row from another table with the same structure):

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

Method 4 (Add row from another table) for adding a row to a DataTable:

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

Method 5 (Insert row at an index) for adding a row to a DataTable:

MyTable.Rows.InsertAt(row, 8);

Answered by Hamed Naeemaei

Solution #4

// 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();

Answered by shahnawaz

Solution #5

To add a row, follow these steps:

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

The following is a diagram of the structure:

Table.Columns

Answered by djdd87

Post is based on https://stackoverflow.com/questions/1042618/how-to-create-a-datatable-in-c-sharp-and-how-to-add-rows