March 16, 2009

After creating a new website or to an existing website, add a new web page and name it DataTable.aspx . Add a GridView control to the page either by dragging the control from the Toolbox or by writing the code manually. Either way, the code for the GridView control will like below.

<body>
<
form id="form1" runat="server">
<
div>

<asp:GridView ID="GridView1"runat="server">


</
asp:GridView>

</div></form></body>

Then, to the code-behind page's Page_Load method write code to create a DataTable.

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable(); //instantiate a new DataTable
}

Then create a new DataColumn object and set its properties.

DataTable dt = new DataTable(); //add DataTable

DataColumn dc1 =new DataColumn("Firstname", typeof(string));// create new DataColumn

dc1.MaxLength = 20;//set the max length property

dc1.Caption = "First Name"; // set the caption

After creating the DataColumn object, add the object to the DataTable

dt.Columns.Add(dc1); // add DataColumn to DataTable

In this example, a DataColumn object, dc1 is created. The column name is set to "FirstName" and the type of object the column accepts is string. If any other object is passed on to the column, an exception will be thrown. The maximum length of the string is 15. If a string longer than 15 characters is passed, the string will be truncated but no exception will be thrown.

Let's add another DataColumn object to the DataTable.

DataColumn dc2 = new DataColumn("LastName", typeof(string));

dc2.MaxLength = 20;
dc2.Caption =
"Last Name";

Then, add the DataColumn to the DataTable,

dt.Columns.Add(dc2); //add DataColumn to DataTable

Let's create another DataColumn of type decimal,

DataColumn dc3 = new DataColumn("Salary", typeof(decimal));//create new DataColumn

dc3.DefaultValue = 0.0;//set the default value

Then, add the DataColumn to the DataTable,

dt.Columns.Add(dc3); //add DataColumn to DataTable

Now, three DataColumn objects are created and added to the DataTable dt.

To add data to the DataTable, DataRow objects needto be created and then added to the Rows collection of the DataTable by using the Add method of the Rows collection. The DataRow object must conform to constraints set by the DataTable object's columns.

To create a new Row for the DataTable, use the NewRow() method on the DataTable.

DataRow row1 = dt.NewRow();//create a new Row

Then add data to the row for individual columns of the DataTable.

DataRow row1 = dt.NewRow();//create a new Row

row1["Firstname"] = "my first name"; //set value for column Firstname
row1["Lastname"]= "my last name"; //set value for column Lastname
row1["Salary"] = 5000; //set value for column Salary

Then, add the DataRow object to the DataTable's Rows collection.

dt.Rows.Add(row1);

Alternatively, a new DataRow object can be added to the DataTable by simply passing values to the DataTable dt.

dt.Rows.Add("my first name2", "my last name", 5000);//add DataRow to DataTable by values


Now, two DataRows have been added to the DataTable dt. The DataTable needs to be binded to the GridView declared in the .aspx page.

First, set the DataSource property of the GridView.

GridView1.DataSource = dt;

Then, execute the DataBind method of the GridView.

GridView1.DataBind();

Now, when the page is run - the page will show a GridView with two rows of data. The whole code is below.

protected
void Page_Load(object sender, EventArgs e)
{

DataTable dt =new DataTable(); //add DataTable

DataColumn dc1 = new DataColumn("Firstname", typeof(string));

// create new DataColumn

dc1.MaxLength = 15;//set the max length property

dc1.Caption = "First Name"; // set the caption

dt.Columns.Add(dc1); // add DataColumn to DataTable

DataColumn dc2 = new DataColumn("LastName", typeof(string));// create new DataColumn

dc2.MaxLength = 20;//set the max length property

dc2.Caption = "Last Name";// set the caption

dt.Columns.Add(dc2);

DataColumn dc3 =new DataColumn("Salary", typeof(decimal));//create new DataColumn

dc3.DefaultValue = 0.0;//set the default value

dt.Columns.Add(dc3); // add dataColumn to DataTable

dt.Rows.Add("my first name2", "my last name", 5000);//add DataRow to DataTable by values

DataRow row1 = dt.NewRow();//create a new Row

row1["Firstname"] = "my first name"; //set value for column Firstname

row1["Lastname"] = "my last name"; //set value for column Lastname

row1["Salary"] = 5000; //set value for column Salary

dt.Rows.Add(row1);

GridView1.DataSource = dt;

GridView1.DataBind();

}


2 comments:

kar said...

Thanks a lot .....

Its very help for me.....

kar said...

Thanks a lot

Reference: Shahed Kazi at AspNetify.com