This
article will show you how you can write code to generate gridview auto
generated row number in asp.net using
c#. In this I have bind the gridview to
populate data in gridview.
Some on my
previous articles are as follows: Bind
TextBox Control Inside TemplateField Of GridView Using C#.Net In Asp.Net, Short
GridView Data From Header Using C#.Net In Asp.Net, Export
GridView Or Table Data Into PDF By C#.Net In Asp.Net Using jQuery, Reading
XML Document in C# Using Linq and Bind To GridView In Asp.Net, How
to Find Gridview Control on Button Click in Asp.Net C#, Nested
GridView Using c#.Net In Asp.Net, Read
XML File in Dataset And Bind To GridView In Asp.Net Using C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net, Comment
System OR Form and Display In GridView Using C# In Asp.Net.
So for this
article first we will create a new asp.net application and add the below code
in the .aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Gridview Auto
Generate Row Number In Asp.Net Using C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Row No.
</HeaderTemplate>
<ItemTemplate>
<%#Container.DataItemIndex
+ 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CountryName"
HeaderText="Country Name"
/>
<asp:BoundField DataField="Population"
HeaderText="Population"
/>
<asp:BoundField DataField="Code" HeaderText="Code" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
Make the AutoGenerateColumns="False” in gridview.
In above
code just check the template field section which is in bold letter.
<asp:TemplateField>
<HeaderTemplate>
Row No.
</HeaderTemplate>
<ItemTemplate>
<%#Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
|
In above
code Container.DataItemIndex + 1 is responsible for
displaying the auto generated row no inside the gridview. Now check he below
code to bind the gridview.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetCountryData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetCountryData()
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("CountryName");
dt.Columns.Add("Population");
dt.Columns.Add("Code");
DataRow dataRow1 =
dt.NewRow();
dataRow1["CountryName"] = "India";
dataRow1["Population"] = "125
Cr";
dataRow1["Code"] = "IN";
dt.Rows.Add(dataRow1);
DataRow dataRow2 = dt.NewRow();
dataRow2["CountryName"] = "Pakistan";
dataRow2["Population"] = "50
Cr";
dataRow2["Code"] = "PK";
dt.Rows.Add(dataRow2);
DataRow dataRow3 =
dt.NewRow();
dataRow3["CountryName"] = "United
States";
dataRow3["Population"] = "25
Cr";
dataRow3["Code"] = "US";
dt.Rows.Add(dataRow3);
return dt;
}
}
}
|
In above code I have bind gridview with datatable. Now we have done run the application to check the output.
0 comments:
Please let me know your view