在gridview中从codebehind绑定标签
本文关键字:绑定 标签 codebehind 中从 gridview | 更新日期: 2023-09-27 18:05:13
我想把我的totalcomplete变量绑定到我的gridProgress中的lblComplete,但我不知道怎么做,有人能帮我吗?
ASP代码:<asp:GridView ID="gridProgress" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Complete">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblComplete" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
c#代码:da = new SqlDataAdapter(sql, oConn);
da.Fill(ds);
//count number of tasks completed, not completed
int count = ds.Tables[0].Rows.Count;
string value = ds.Tables[0].Rows[0]["Completed"].ToString();
int completed = 0;
for (int i = 0; i < count; i++)
{
if (value == "No")
{
completed++;
}
int totalcomplete = completed;
//here i want to bind totalcomplete to my lblCompleted
}
使用Gridview
的FindControl
属性
Label lbl = gv.Rows[rowIndex].FindControl("lblComplete") as Label;
lbl.Text = Convert.ToString(totalcomplete);
你的问题不清楚。
是否要在每行中显示totalalcomplete
还是去别的地方?
在gridview中有一个模板字段
所以它会在每行
你的循环也很奇怪
应该如下
int count = ds.Tables[0].Rows.Count;
int completed = 0;
for (int i = 0; i < count; i++)
{
string value = ds.Tables[0].Rows[i]["Completed"].ToString();
if (value == "No")
{
completed++;
}
int totalcomplete = completed;
//here i want to bind totalcomplete to my lblCompleted
}
编辑1
这个链接可能对你有帮助
在GridView的页脚显示总数,并在最后一列中添加列的总和(行vise)
http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html
您可以使用DataBinder.Eval method
Text='<%# DataBinder.Eval(Container.DataItem,"totalcomplete") %>'