Data Table is roughly equivalent to a table in a database. DataSet contains many Data Tables. It is a class and stores only one table. DataTable is an in-memory representation of a single database table which has the collection of columns and rows. It fetches only one row at a time. In DataTable, there is no unique key and foreign key constraints are available. In DataTable, the data source can’t be serialized.
DataTable
Get the data from the database and return as DataTable and create a new instance of DataTable.
- DataTable dtbranch1 = new DataTable();
- dtbranch1 = ObjCom.methodName(Obj);
dtbranch1
| No | Cre | Deb |
| 1 | ||
How can we change the column name of DataTable? If DataTable has more than one row, then change the column names.
- if (dtbranch1.Rows.Count > 0) {
- dtbranch1.Columns["Cre"].ColumnName = "CREDIT";
- dtbranch1.Columns["Deb"].ColumnName = "DEBIT";
- dtbranch1.AcceptChanges();
- }
| No | CREDIT | DEBIT |
| 1 | ||
How can we change the DataTypes? If we want to change the DataType of a column, then we should clone the DataTable and create a new instance of datatable, as shown below.
- dtbranch1.AcceptChanges();
- DataTable dtbranch = dtbranch1.Clone();
- dtbranch.Columns["CREDIT"].DataType = typeof(decimal);
- dtbranch.Columns["DEBIT"].DataType = typeof(decimal);
- foreach(DataRow row in dtbranch1.Rows) {
- dtbranch.ImportRow(row);
- }
| No | CREDIT | DEBIT |
| 1 | ||

Viknaraj ManogararajahPosted Jul 21, 2018, 11:00 PM
Nice Article, Thank you for sharing