DataTable employee = new DataTable("Employee");
//Add the DataColumn using all properties
DataColumn eid = new DataColumn("Eid");
eid.DataType = typeof(string);
eid.MaxLength = 10;
eid.Unique = true;
eid.AllowDBNull = false;
eid.Caption = "EID";
employee.Columns.Add(eid);
//Add the DataColumn using defaults
DataColumn firstName = new DataColumn("FirstName");
firstName.MaxLength = 35;
firstName.AllowDBNull = false;
employee.Columns.Add(firstName);
DataColumn lastName = new DataColumn("LastName");
lastName.AllowDBNull = false;
employee.Columns.Add(lastName);
//Add the decimal DataColumn using defaults
DataColumn salary = new DataColumn("Salary", typeof(decimal));
salary.DefaultValue = 0.00m;
employee.Columns.Add(salary);
//Derived column using expression
DataColumn lastNameFirstName = new DataColumn("LastName and FirstName");
lastNameFirstName.DataType = typeof(string);
lastNameFirstName.MaxLength = 70;
lastNameFirstName.Expression = "lastName + ', ' + firstName";
employee.Columns.Add(lastNameFirstName);
//Add New DataRow by creating the DataRow first DataRow newemployee = employee.NewRow(); newemployee["Eid"] = "123456789A"; newemployee["FirstName"] = "Nancy"; newemployee["LastName"] = "Davolio"; newemployee["Salary"] = 10.00m; employee.Rows.Add(newemployee);
//Add New DataRow by simply adding the values employee.Rows.Add("987654321X", "Andrew", "Fuller", 15.00m);
//Load DataRow, replacing existing contents, if existing
employee.LoadDataRow(
new object[] { "987654321X", "Janet", "Leverling", 20.00m },
LoadOption.OverwriteChanges);
GridView1.DataSource = employee;
GridView1.DataBind();