How do I create a DataTable, then add rows to it?

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

Leave a Comment