从后面的代码绑定gridview中的标签。

时间:2013-03-22 14:28:01

标签: c# asp.net gridview

我想将我的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
}

3 个答案:

答案 0 :(得分:1)

使用FindControl的{​​{1}}属性。

Gridview

答案 1 :(得分:1)

你的问题不明确。
是否要在每行中显示totlalComplete 或者别的地方?
因为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

这是一个可以帮助您的链接 Displaying Total in Footer of GridView and also Add Sum of columns(row vise) in last Column
http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html

答案 2 :(得分:0)

您可以使用DataBinder.Eval method

Text='<%# DataBinder.Eval(Container.DataItem,"totalcomplete") %>'