Creating a DataTable | DataTable in C#
DataTable aTable = new DataTable();
aTable.Columns.Add("ProductID", typeof(int));
aTable.Columns.Add("ProductName", typeof(string));
DataRow dr;
dr = aTable.NewRow();
dr[0] = 12;
dr[1] = "Lap Top";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 13;
dr[1] = "DeskTop";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 14;
dr[1] = "Server";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 15;
dr[1] = "Mac PC";
aTable.Rows.Add(dr);
You can create these rows by getting data from database also
command=new SqlCommand("select * from Product",connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
Dr=aTable.NewRow();
Dr[0] = reader.GetValue(1);
Dr[1] = reader.GetValue(2);
aTable.Rows.Add(Dr);
}
Subscribe to:
Post Comments (Atom)
-
Clustered Index | Non Clustered Index Indexing is used to get fast retrival of data. The indexing which is done on physical storage is known...
-
All about Haj, please visit http://www.hajcommittee.com This year UP haj pilgrims will be decided on lottery draw due to more responces of l...
-
To get all the triggers list and their scripts run the script below. View the result in file view mode. SELECT [text] FROM sysobjects o ...
hey,
ReplyDeletethis is sweet and simple but can u tell where a data table is used or how the data in data table is used
You can use datatable as per your need,
ReplyDeleteEither you can loop through the datatable for presnting into a row column manner, or you can use as a datasource for Datagridview.
Also you can add this table into a dataset, perform queries on that dataset and display the results to some UI.
loudaka ball,na madda first u have to write definition useless fellow
ReplyDeletelanzada kodaka etla konjura yodambu doma kuchuka sacchen
ReplyDelete