Click here to Skip to main content
14,767,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know how to add row no in gridview using the below code snippet

<asp:GridView ID="gvLeaveRequest" runat="server" DataKeyField="AID" AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True" GridLines="Both" DataKeyNames="AID" CssClass="gvTable"
AllowPaging="True" PagerSettings-Mode="NextPreviousFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last"
PagerSettings-NextPageText="Next" PagerSettings-PreviousPageText="Previous" PageSize="75" OnPageIndexChanging="gvLeaveRequest_PageIndexChanging">
<AlternatingRowStyle BackColor="#FFFFFF" ForeColor="#000000"/>
<ItemTemplate><%#Container.DataItemIndex + 1 %></ItemTemplate>
<asp:BoundField DataField="APStartDateVal" HeaderText="Calendar Start Date" />
<asp:BoundField DataField="APEndDateVal" HeaderText="Calendar End Date" />
<asp:BoundField DataField="EmpFullName" HeaderText="Employee Name" />
<asp:BoundField DataField="LeaveStartDateVal" HeaderText="Start Date" />
<asp:BoundField DataField="LeaveEndDateVal" HeaderText="End Date" />
<asp:BoundField DataField="LeaveStartTimeVal" HeaderText="Start Time" />
<asp:BoundField DataField="LeaveEndTimeVal" HeaderText="End Time" />
<asp:BoundField DataField="LeaveCount" HeaderText="Leave Count" />
<asp:BoundField DataField="LTName" HeaderText="Leave Type" />
<asp:BoundField DataField="LDName" HeaderText="Leave Duration" />
<asp:BoundField DataField="LSName" HeaderText="Leave Status Name" />
<asp:BoundField DataField="LeaveReason" HeaderText="Reason" />
<asp:BoundField DataField="DateAppliedOn" HeaderText="Date Applied On" />
<asp:HyperLinkField DataNavigateUrlFields="AID,EID" Text="Edit" HeaderText="Edit" DataNavigateUrlFormatString="UpdatePastLeaves.aspx?lvid={0}&eid={1}" />
<asp:HyperLinkField DataNavigateUrlFields="AID,EID" Text="Cancel" HeaderText="Cancel" DataNavigateUrlFormatString="CancelPastLeaves.aspx?lvid={0}&eid={1}" />
<asp:HyperLinkField DataNavigateUrlFields="AID,EID" Text="Delete" HeaderText="Delete" DataNavigateUrlFormatString="DeletePastLeaves.aspx?lvid={0}&eid={1}" />
<EditRowStyle BackColor="#D3D3D3" />
<FooterStyle BackColor="#A8A8A8" Font-Bold="True" ForeColor="#000000" />
<HeaderStyle BackColor="#A8A8A8" Font-Bold="True" ForeColor="#000000" />
<PagerStyle BackColor="#A8A8A8" HorizontalAlign="Center" ForeColor="#000000"/>
<RowStyle BackColor="#D3D3D3" />
<SelectedRowStyle BackColor="#D3D3D3" Font-Bold="True" ForeColor="#000000" />

Is there any way using c# code to add row number column to gridview?

This below code snippet does the same thing using syntax.
<ItemTemplate><%#Container.DataItemIndex + 1 %></ItemTemplate>

Please help

What I have tried:

I dont want to use row databound event to achieve this. I need c# code for this.
Updated 26-Mar-19 9:50am

The GridView control will generate data based on it's datasource, so technically you could manipulate the datasource and add whatever you like in there before populating the grid. For example, if you are using DataTable, you could construct the Rows and Columns dynamically and populate them with data:

DataTable dt = new DataTable();
DataRow dr = null;

dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));value
// add as many as you like

dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;

//Bind the Gridview
Gridview1.DataSource = dt;

If you have an existing DataTable and would like to add a new Column for your row number then you have to iterate to each DataRow and append the row number:

DataTable source = ???; //Set the datasource here
DataColumn newCol = new DataColumn("RowNumber", typeof(string));
int i = 0;
foreach (DataRow row in source.Rows) {
    row["RowNumber"] = i.ToString();

With that, you can then set the RowNumber column as DataField value like this:

<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
Knowing nothing else about your app, I would create a subclass with the id added; or, easier, create a "conainer" class to hold any new field(s) and a reference to the "old" "record".

public class xxx {
   public int Index {get;set;}
   public Foo DataItem {get;set;}

Reference DataItem properties using dot notation:

... DataField="DataItem.Axxxx" ...

Use LINQ, or whatever, to create the "items source" components from the orignal.

Or use a DataSet / DataTable.

Or SQL ... depending on your original data source.

What did I leave out?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900