Most of the times programmers fill the DataTable from database. This action fills the schema of the database table into the DataTable. We can bind data controls like GridView, DropdownList to such DataTable. But somtimes what happens is one needs to create a DataTable programmatically. Here I have put the simple method of creating DataTable programmatically in C#.
Update: Responding to comment from one of the readers, I have now posted how to create DataTable in c# with clickable column. As in this post, the DataTable has been bound to an asp.net GridView.
Create a DataTable instance
DataTable table = new DataTable();
Create 7 columns for this DataTable
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Checked");
DataColumn col4 = new DataColumn("Description");
DataColumn col5 = new DataColumn("Price");
DataColumn col6 = new DataColumn("Brand");
DataColumn col7 = new DataColumn("Remarks");
Define DataType of the Columns
col1.DataType = System.Type.GetType("System.Int");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.Boolean");
col4.DataType = System.Type.GetType("System.String");
col5.DataType = System.Type.GetType("System.Double");
col6.DataType = System.Type.GetType("System.String");
col7.DataType = System.Type.GetType("System.String");
Add All These Columns into DataTable table
Create a Row in the DataTable table
DataRow row = table.NewRow();
Fill All Columns with Data
row[col1] = 1100;
row[col2] = "Computer Set";
row[col3] = true;
row[col4] = "New computer set";
row[col5] = 32000.00
row[col6] = "NEW BRAND-1100";
row[col7] = "Purchased on July 30,2008";
Add the Row into DataTable
Want to bind this DataTable to a GridView?
GridView gvTest=new GridView();
gvTest.DataSource = table;
You are done! Happy dot netting!!
Most of the times programmers fill the DataTable from database. This action fills the schema of the database table into the DataTable . We ...
I have oracle database installed in my computer. My Operating system is XP. I wrote an application in asp.net. It connected to oracle databa...
The asp.net Eval() and Bind() expressions are heavily used in asp.net GridView and DetailsView data bound controls. The difference between ...
Last time I got the following error: HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related ...
We do have a folder (and a number of sub folders) with a number of various files in those folders (and sub folders). Now we do need to list ...
Much often we open child windows from parent web page . In the child page we perform some activities, and later close the window. At the ver...
The default behaviour of asp.net gridview is that it hides both Gridview header and footer when no data is present. You can note the absence...