【问题标题】:Binding label inside a gridview from codebehind.来自代码隐藏的网格视图内的绑定标签。
【发布时间】:2013-03-22 14:28:00
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    使用GridviewFindControl 属性。

    Label lbl = gv.Rows[rowIndex].FindControl("lblComplete") as Label;
    lbl.Text = Convert.ToString(totalcomplete); 
    

    【讨论】:

      【解决方案2】:

      您的问题不清楚。
      是否要在每一行中显示 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

      【讨论】:

        【解决方案3】:

        你可以使用DataBinder.Eval method

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

        【讨论】:

          猜你喜欢
          • 2013-08-08
          • 1970-01-01
          • 1970-01-01
          • 2017-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多