Creating a datatable and binding it to datagrid: First DataTable is created,then columns are added to table and finally rows are added to the table.
//Creates a new datatable named “Details” and dataset
DataTable objDataTable=new DataTable("Details"); DataSet objDataSet=new DataSet();
//Adding "Details" table to the dataset.
objDataSet.Tables.Add(objDataTable);
/*Following code adds the columns “Name”,”Address”,”City”,”Pin” and its datatypes to the created data table.*/
objDataTable.Columns.Add("Name",typeof(string)); objDataTable.Columns.Add("Address",typeof(string)); objDataTable.Columns.Add("City",typeof(string)); objDataTable.Columns.Add("Pin",typeof(Int32));
//Creates a new datarow for the table objDataTable
DataRow objDR=objDataTable.NewRow(); //The following code adds the data to the table. objDR ["Name"]="X"; objDR ["Address"]="Thiruvanmiyur"; objDR ["City"]="Chennai"; objDR [“Pin”]=600040;
//Adding the created data row to the datatable
objDataTable.Rows.Add(objDR);
//The following code binds “Details” table present in DataSet to DataGrid
DataGrid1.SetDataBinding(objDataSet,”Details”);
|
No responses found. Be the first to respond and make money from revenue sharing program.
|