Using GridView in ASP.NET & C# — PART 1
GridView displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items. In this article I would discuss some basic scenarios using GridView.
Example 1: Bind the Gridview with DataSet
Step 1: Create a new ASP.NET Web Application in Visual Studio
Step 2: I am using AdventureWorks for my samples. Open web.config and add following entry to connection strings element
<add name= "Sql" connectionString="Data Source=<SERVERNAME>; |
Initial Catalog= AdventureWorks;User=testuser;Password=testuser;" |
providerName= "System.Data.SqlClient" /> |
Step 3: Open aspx file in design mode and add the following in div tag
<asp:gridview id= "gvProducts" |
autogeneratecolumns= "False" |
emptydatatext= "No data available." |
runat= "server" DataKeyNames= "ProductID" > |
<asp:BoundField DataField= "ProductID" HeaderText= "Product ID" /> |
<asp:BoundField DataField= "Name" HeaderText= "Product Name" /> |
<asp:BoundField DataField= "ProductNumber" HeaderText= "Product Number" /> |
<asp:BoundField HeaderText= "Price" |
DataFormatString= "{0:c}" > |
<ItemStyle HorizontalAlign= "Right" ></ItemStyle> |
Name | Description |
AllowPaging | Gets or sets a value indicating whether the paging feature is enabled. |
autogeneratecolumns | Gets or sets a value indicating whether bound fields are automatically created for each field in the data source. |
emptydatatext | Gets or sets the text to display in the empty data row rendered when a GridView control is bound to a data source that does not contain any records. |
DataKeyNames | Gets or sets an array that contains the names of the primary key fields for the items displayed in a GridView control. |
BoundField | Displays the value of a field in a data source. This is the default column type of the GridView control. |
BoundField -> DataField | Gets or sets the name of the data field to bind to the BoundField object. |
BoundField -> HeaderText | Gets or sets the text that is displayed in the header of a data control |
BoundField -> DataFormatString | Gets or sets the string that specifies the display format for the value of the field. |
ItemStyle | Gets the style of any text-based content displayed by a data control field. |
Step 4: Open Default.aspx.cs and add the following code in the page_load function
string connString = ConfigurationManager.ConnectionStrings[ "Sql" ].ConnectionString; |
string sql = @"Select top 10 P.ProductID, P.Name, |
P.ProductNumber, ListPrice from Production.Product P |
where ProductSubcategoryID is not null" ; |
DataSet dsResults = new DataSet(); |
using (SqlConnection conn = new SqlConnection(connString)) |
using (SqlCommand cmd = new SqlCommand(sql, conn)) |
SqlDataAdapter adapter = new SqlDataAdapter(cmd); |
gvProducts.DataSource = dsResults; |
Step 5: Run the application
Output:
Example 2: Bind the GridView with SQL DataReader
Step 1: Repeat Steps 1 to Step 3 from “Bind the Gridview with DataSet” section
Step 2: Open Default.aspx.cs and add the following code in the page_load function
string connString = ConfigurationManager.ConnectionStrings[ "Sql" ].ConnectionString; |
string sql = @"Select top 10 P.ProductID, P.Name, |
P.ProductNumber, ListPrice from Production.Product P |
where ProductSubcategoryID is not null" ; |
SqlDataReader reader = null ; |
using (SqlConnection conn = new SqlConnection(connString)) |
using (SqlCommand cmd = new SqlCommand(sql, conn)) |
reader = cmd.ExecuteReader(); |
gvProducts.DataSource = reader; |
Step 3: Run the application.
Output
No comments:
Post a Comment