How to bind data to different gridview controls - without database.
In this article we are going to focus how to bind data to different grid controls in ASP.NET using C# in the page load event. The page Load event is triggered before the Load event for any controls. Gridview control displays the values in a table where each column represents a field and each row represents a record.
Here I'll show you how to bind data to grid controls in 5 different ways and it is familiar to all.
1.Gridview
2.DataList
3.Details View
4.List View
5.Repeater
<br>
1.Binding record using Grid view. Both in server-side and client-side.
=============================================================================
<h1> Grid View </h1>
<asp:GridView ID="Grd" runat="server" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
DataTable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Empno");
Dt.Columns.Add("Amount");
DataRow dr;
dr = Dt.NewRow();
dr[0] = "Jayakumar";
dr[1] = 150;
dr[2] = 7500.25;
Dt.Rows.Add(dr);
Grd.DataSource = Dt;
Grd.DataBind();
2.Binding record using Datalist. Both in server-side and client-side
===============================================================================
<h1> Data List </h1>
<asp:DataList ID="DataList1" runat="server" BackColor="Gray" BorderColor="#666666"
BorderStyle="None" BorderWidth="2px" CellPadding="3" CellSpacing="2"
Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="3" RepeatDirection="Horizontal"
Width="600px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#333333" Font-Bold="True" Font-Size="Large" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderTemplate>
Customer Details</HeaderTemplate>
<ItemStyle BackColor="White" ForeColor="Black" BorderWidth="2px" />
<ItemTemplate>
<b> Name:</b>
<asp:Label ID="lblCName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<br />
<b>Empno:</b>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Empno") %>'></asp:Label>
<br />
<b> Amount:</b>
<asp:Label ID="lblCity" runat="server" Text=' <%# Eval("Amount") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:DataList>
DataTable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Empno");
Dt.Columns.Add("Amount");
DataRow dr;
dr = Dt.NewRow();
dr[0] = "Jayakumar";
dr[1] = 150;
dr[2] = 7500.25;
Dt.Rows.Add(dr);
DataList1.DataSource = Dt;
DataList1.DataBind();
3.Binding record using Details view. Both in server-side and client-side
=================================================================================
<h1> Details View </h1>
<asp:DetailsView ID="Dtv" runat="server" BackColor="White" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None">
<EditRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
</asp:DetailsView>
DataTable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Empno");
Dt.Columns.Add("Amount");
DataRow dr;
dr = Dt.NewRow();
dr[0] = "Jayakumar";
dr[1] = 150;
dr[2] = 7500.25;
Dt.Rows.Add(dr);
Dtv.DataSource = Dt;
Dtv.DataBind();
4.Binding record using List view. Both in server-side and client-side
======================================================================
<h1> ListView </h1>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<td>
<table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px;
border: dashed 2px #04AFEF; background-color: #B0E2F5">
<tr>
<td>
<b><u><span class="name">
<%# Eval("Name") %><span><u><b>
<td>
<tr>
<tr>
<td>
<b>Name:<span class="Name"><%# Eval("Name") %><span><br />
<b>Empno:<span class="Empno"><%# Eval("Empno") %><span><br />
<b>Amount:<span class="Amount"><%# Eval("Amount")%><span><br />
<td>
<tr>
<table>
<td>
</ItemTemplate>
</asp:ListView>
DataTable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Empno");
Dt.Columns.Add("Amount");
DataRow dr;
dr = Dt.NewRow();
dr[0] = "Jayakumar";
dr[1] = 150;
dr[2] = 7500.25;
Dt.Rows.Add(dr);
ListView1.DataSource = Dt;
ListView1.DataBind();
5.Binding record using a Repeater.
=============================================
<h1> Repeater </h1>
<asp:Repeater ID="Rep" runat ="server" >
<HeaderTemplate>
<table cellpadding="1" cellspacing="1" width="100%" style="font-family:Verdana;border:1px solid #C0C0C0;background-color:#D8D8D8">
<tr style="background-color:#FF781E">
<th>
ID
</th>
<th>
Customer
</th>
<th>
Date
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:White">
<td>
<asp:TextBox ID="id" runat="Server" Text='<%#Eval("Name")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="Cus_name" runat="Server" Text='<%#Eval("Empno")%>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="Server" Text='<%#Eval("Amount")%>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
DataTable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Empno");
Dt.Columns.Add("Amount");
DataRow dr;
dr = Dt.NewRow();
dr[0] = "Jayakumar";
dr[1] = 150;
dr[2] = 7500.25;
Dt.Rows.Add(dr);
Rep.DataSource = Dt;
Rep.DataBind();
See the image given below.
<img src='/attachments/Resources/45860-83720-Bind-Records-Different-Controls-in-Aspnet.png' alt='Bind Records Different Controls in Asp.net' style='padding:10px' >