Ways of Adding rows into DataTable.

vishalneeraj-24503
Posted by vishalneeraj-24503 under C# category on | Points: 40 | Views : 1560
There are 2 ways to add Rows into any DataTable.

For Example:
1st way:-
//Directly using DataTable Row' Add static method

DataTable dt_employee = new DataTable();

dt_employee.Columns.Add("Name");
dt_employee.Columns.Add("Address");

dt_employee.Rows.Add("Vishal","Neeraj");

2nd way:-
//DataTable NewRow() method with DataRow

DataRow dr_employee = null;

//assign values based on column indexes

dr_employee = dt_employee.NewRow();
dr_employee[0] = "Rajesh";
dr_employee[1] = "Nagpur";
dt_employee.Rows.Add(dr_employee);

//or assign values by using column names

dr_employee = dt_employee.NewRow();
dr_employee["Name"] = "Nitin";
dr_employee["Address"] = "Indore";
dt_employee.Rows.Add(dr_employee);

Comments or Responses

Login to post response