Programmingempire
The following example shows Creating Columns in a DataGrid Control. Before going through this example, get an overview of DataGrid control by reading this article.
As I have noted earlier, our SQL Server database has a table called Items with fields – item_id, item_name, price, quantity. Therefore, we create four TemplateColumns, one for each field. In each TemplateColumn, we create a Label to show the value of a particular field. Further, in each TemplateColumn, we can add a HeaderTemplate, and an ItemTemplate to display the content of that field along with the header.
The following code shows Creating Columns in a DataGrid Control.
As shown below, each ItemTemplate contains a Label control. Further, the Label controls are using a data binding expression <%#…%> for their Txt property. Also, the Eval() function in a data binding expression fetches the value of a field.
<asp:DataGrid ID="DataGrid1" runat="server"
AutoGenerateColumns="false" DataKeyField="item_id"
BackColor="Lavender" Font-Bold="true" BorderColor="Navy"
BorderStyle="Outset" BorderWidth="4"
CellPadding="10" CellSpacing="10"
Font-Size="Large">
<Columns>
<asp:TemplateColumn>
<HeaderStyle BackColor="LightBlue" ForeColor="Navy"/>
<HeaderTemplate>
Item ID
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%#DataBinder.Eval(Container,"DataItem.item_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle BackColor="LightBlue" ForeColor="Navy"/>
<HeaderTemplate>
Item Name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#DataBinder.Eval(Container,"DataItem.item_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle BackColor="LightBlue" ForeColor="Navy"/>
<HeaderTemplate>
Price
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%#DataBinder.Eval(Container,"DataItem.price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle BackColor="LightBlue" ForeColor="Navy"/>
<HeaderTemplate>
Quantity
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%#DataBinder.Eval(Container,"DataItem.quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
However, we must make a connection with the database, and bind the DataGrid control to that data. Therefore, we need to write the following code also.
using System;
using System.Data;
using System.Data.SqlClient;
namespace DataGridExamples
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection c1;
SqlDataAdapter da;
DataSet ds;
public void Connect()
{
String str="write your connection string here";
c1 = new SqlConnection(str);
c1.Open();
}
public void BindGrid()
{
Connect();
da = new SqlDataAdapter("select * from Items", c1);
ds = new DataSet("ItemsData");
da.Fill(ds, "Items");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
}
}
As a result of execution, we get the following output.
As shown above, make a connection with the database using the given connection string and open the connection. Further, create a DataSet object and fill it with the data retrieved by running the select query. After that, perform data binding.
Further Reading
Parameter and ParameterCollection in ADO.NET
Database Manipulation Using DataGrid
Example of Button and Link Button Control in ASP.NET
Example of Chart Control in ASP.NET
Creating a DataTable from a DataReader in ASP.NET
Deleting a Record using DataGrid Control in ASP.NET
Edit a Record Using DataGrid Control in ASP.NET
Insert a Record Using ItemCommand Event in DataGrid
CRUD Operations with DataGrid in ASP.NET
Creating Columns in a DataGrid Control
XML Documents and DataSet in ASP.NET
ASP.NET Core Features and Advantages
Display Images Using DataList Control
Adding Images Using Image Control
Creating a Group of Radio Buttons Using RadioButtonList Control
Example of Button Control in ASP.NET
ItemDataBound Event in DataList
More Features of DataList in ASP.NET
A Simple Example of Using a DataList Control in ASP.NET
Properties and Methods of DataList Control in ASP.NET
Exploring DataList Control in ASP.NET
Custom Validator Control in ASP.NET
Validation Summary Control in ASP.NET
Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator
An Example of Data Binding with RadioButtonList Control
Binding Data to Web Control in ADO.NET
Examples of AdRotator Control in ASP.NET