我使用asp:SqlDataSource控件,从数据库Select某个表的全部记录绑定到GridView控件上,但是还想在GridView的第一列显示行号,1,2,3,4……
<asp:SqlDataSource ID="sqlRegister" runat="server" ConnectionString="<%$ ConnectionStrings:websiteDBConnectionString %>"
SelectCommand = "Select ID, Name, Sex, ApplyJob, AddTime From WS_Register ">
</asp:SqlDataSource><asp:GridView ID=gv runat=server>
<asp:BoundColumn DataField=SeqID Visible=true></asp:BoundColumn>
……
</asp:GridView>我开始想通过在GridView的RowDataBound事件中给SeqID列赋值,如下所示:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是数据项并且是交替项
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
{
//给SeqID字段绑定值
int seqID = gv.PageSize * gv.PageIndex + gv.Rows.Count + 1;
e.Row.Cells[0].Text = seqID.ToString();
}
} 但是运行到数据绑定时出错,提示为:“未在指定的数据源中找到SeqID”,这是不是意味着,我必须在SqlDataSource的SelectCommand中增加SeqID字段啊,Sql语句应该怎么写呢?
如果不需要这样做,那又怎样为这个模板列赋值?
请各位高手指教!
<asp:SqlDataSource ID="sqlRegister" runat="server" ConnectionString="<%$ ConnectionStrings:websiteDBConnectionString %>"
SelectCommand = "Select ID, Name, Sex, ApplyJob, AddTime From WS_Register ">
</asp:SqlDataSource><asp:GridView ID=gv runat=server>
<asp:BoundColumn DataField=SeqID Visible=true></asp:BoundColumn>
……
</asp:GridView>我开始想通过在GridView的RowDataBound事件中给SeqID列赋值,如下所示:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是数据项并且是交替项
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
{
//给SeqID字段绑定值
int seqID = gv.PageSize * gv.PageIndex + gv.Rows.Count + 1;
e.Row.Cells[0].Text = seqID.ToString();
}
} 但是运行到数据绑定时出错,提示为:“未在指定的数据源中找到SeqID”,这是不是意味着,我必须在SqlDataSource的SelectCommand中增加SeqID字段啊,Sql语句应该怎么写呢?
如果不需要这样做,那又怎样为这个模板列赋值?
请各位高手指教!
解决方案 »
- textbox赋值刷新内容丢失的问题
- 如何在脚本中取datagrid中的某一列的值?
- [相容性問題] 遇到版面設計問題~!!! 無法為網站定位~!!!
- 有没有办法去掉免费空间的广告条.
- 关于frameset的问题请教
- 再发关于word文件导入弹出登录小窗口的问题
- ASP.NET系统,想用CheckBoxList和字符串做权限管理,应该怎么做
- .net使用confirm提示不能给隐藏input赋值。
- ®如何在一个网页中对另一个网页进行刷新?
- 如何在自己的类中定义一个enum,让程序在使用这个类的时候可以使用
- c# asp.net调用非托管代码
- 请问如何动态模拟 Windows 帐户(即如何使用impersonate方法)?
在ItemDataBound事件里写
int count =GetUpLoadFile.CurrentPageIndex;
int size = GetUpLoadFile.PageSize * count;
int result = size + e.Item.ItemIndex+1;
System.Web.UI.WebControls.Label lab = (System.Web.UI.WebControls.Label)e.Item.Cells[0].FindControl("Label1");
if(lab!=null)
{
lab.Text= result.ToString();
}
private int intRow=0;//****构造一个自增变量函数
public int AutoRowNumber()
{
return intRow++;
}
//*****前台邦定,datagive模版列中邦定即可//****例如:
<asp:GridView ID=GView runat=server AutoGenerateColumns="False" OnRowCommand="GView_RowCommand" OnRowEditing="GView_RowEditing" OnRowCancelingEdit="GView_RowCancelingEdit" OnRowDataBound="GView_RowDataBound" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="GView_PageIndexChanging" PageSize="2">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%=AutoRowNumber()%>
</ItemTemplate>
</asp:TemplateField>
</columns>
<asp:datagive/>