将行号列动态添加到ASP.NET gridview [英] Dynamically add row number column to ASP.NET gridview

查看:58
本文介绍了将行号列动态添加到ASP.NET gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用下面的代码片段在asp.net gridview中添加行号



I know how to add row no in asp.net 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"/>
<Columns>
<asp:TemplateField>
<HeaderTemplate>#</HeaderTemplate>
<ItemTemplate><%#Container.DataItemIndex + 1 %></ItemTemplate>
</asp:TemplateField>
<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}" />
</Columns>
<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" />
</asp:GridView>





有没有办法使用c#代码将行号列添加到asp.net gridview?



下面的代码片段使用asp.net语法做同样的事情。



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

This below code snippet does the same thing using asp.net syntax.

<asp:TemplateField>
<HeaderTemplate>#</HeaderTemplate>
<ItemTemplate><%#Container.DataItemIndex + 1 %></ItemTemplate>
</asp:TemplateField>





请帮助



我尝试过:



我不想使用行数据绑定事件来实现这一点。我需要c#代码。



Please help

What I have tried:

I dont want to use row databound event to achieve this. I need c# code for this.

推荐答案

GridView 控件将根据它的<$ c $生成数据c> datasource ,所以从技术上讲,你可以操作数据源并在填充网格之前添加你喜欢的任何内容。例如,如果您使用 DataTable ,则可以构造动态地用数据填充它们:



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;
dt.Rows.Add(dr);

//Bind the Gridview
Gridview1.DataSource = dt;
Gridview1.DataBind();





如果您有现有的 DataTable 并希望为您的行号添加一个新的,然后您必须迭代到每个 DataRow 并附加行号:





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));
tbl.Columns.Add(newCol);
int i = 0;
foreach (DataRow row in source.Rows) {
    i++;
    row["RowNumber"] = i.ToString();
}





有了这个,你可以设置 RowNumber 列为 DataField 这样的值:





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

<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />


了解您的应用程序,我会创建一个添加了id的子类;或者,更容易,创建一个conainer类来保存任何新字段和对旧记录的引用。



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;}
}





使用点表示法引用DataItem属性:





Reference DataItem properties using dot notation:

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





使用LINQ或其他方法从orignal创建items source组件。



或使用DataSet / DataTable。



或SQL ...取决于您的原始数据源。



我遗漏了什么?



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?

<无线电通信/>

这篇关于将行号列动态添加到ASP.NET gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆