Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 14, 2009 04:58 PM by ecbruck
Member
115 Points
99 Posts
May 14, 2009 04:41 PM|LINK
HI,
I have a gridview that has 4 columns : Place, Population,AverageAge, AvgSalary . I am binding this gridview to a datatable.
I want to added another column to this gridviews which should show the serial number of each row .
The reason i wanted this is my grid shows about 400 records and it is difficult for the user to point the record he wants to reffer to.
so if I can have a serial number column ( like excell) it will be easy for the user.
this serial number should be indepenent of the columns in database table and the where condition in select query
can anyone please help me out with this
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
May 14, 2009 04:46 PM|LINK
Hi,
I didn't really get the serial number thing. Is it something generated by you in code behind that you want to bind to your grid?
If you simply want to show the number of the row in the grid you can check out this article: Autonumbering ASP.NET grid controls.
Grz, Kris.
98783 Points
9691 Posts
May 14, 2009 04:58 PM|LINK
Here's an example for you:
ASPX
<%@ page title="" language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true" codefile="AddRowNumberAsAddedColumn.aspx.cs" inherits="GridView_AddRowNumberAsAddedColumn" %> <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server"> <asp:gridview id="GridView1" runat="server" allowpaging="True" onpageindexchanging="GridView1_PageIndexChanging" pagesize="5"> <columns> <asp:templatefield> <itemtemplate> <%# Container.DataItemIndex + 1 %> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </asp:content>
CODE-BEHIND
using System; using System.Web.UI.WebControls; public partial class GridView_AddRowNumberAsAddedColumn : System.Web.UI.Page { private void GetData() { GridView1.DataSource = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' }; GridView1.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.GetData(); } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; this.GetData(); } }
developer_zo...
Member
115 Points
99 Posts
How to get Serial Number in gridview
May 14, 2009 04:41 PM|LINK
HI,
I have a gridview that has 4 columns : Place, Population,AverageAge, AvgSalary . I am binding this gridview to a datatable.
I want to added another column to this gridviews which should show the serial number of each row .
The reason i wanted this is my grid shows about 400 records and it is difficult for the user to point the record he wants to reffer to.
so if I can have a serial number column ( like excell) it will be easy for the user.
this serial number should be indepenent of the columns in database table and the where condition in select query
can anyone please help me out with this
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: How to get Serial Number in gridview
May 14, 2009 04:46 PM|LINK
Hi,
I didn't really get the serial number thing. Is it something generated by you in code behind that you want to bind to your grid?
If you simply want to show the number of the row in the grid you can check out this article: Autonumbering ASP.NET grid controls.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: How to get Serial Number in gridview
May 14, 2009 04:58 PM|LINK
Here's an example for you:
ASPX
<%@ page title="" language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true" codefile="AddRowNumberAsAddedColumn.aspx.cs" inherits="GridView_AddRowNumberAsAddedColumn" %> <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server"> <asp:gridview id="GridView1" runat="server" allowpaging="True" onpageindexchanging="GridView1_PageIndexChanging" pagesize="5"> <columns> <asp:templatefield> <itemtemplate> <%# Container.DataItemIndex + 1 %> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </asp:content>CODE-BEHIND
Microsoft MVP - ASP.NET