如何遍历每一行,将值放在gridview列单元格中

时间:2018-08-16 07:10:57

标签: c# asp.net gridview

我有3列100行的gridview。还有一个提交按钮不在gridview中。

这里的前2列是有界字段,第3列是Label(模板字段)。

现在,我想更改第三列的状态,并在循环运行时将其显示到gridview。

我要

when i=0 then i want to change label's value to "SUCESS" and display on Gridview,
     i=1 then i want to change label's value to "SUCESS" and display on Gridview,
     and so on till i=100.

Gridview

<asp:GridView ID="GvCategoryLive" runat="server" AutoGenerateColumns="False" EmptyDataText="No Records Found !"  >
  <Columns>
      <asp:BoundField DataField="UnitName" HeaderText="Unit Name" />
      <asp:BoundField DataField="CreateDate" HeaderText="Created Date" />
      <asp:TemplateField HeaderText="Status">
          <ItemTemplate>
              <asp:Label ID="UnitMesurement_Id" runat="server" Text=""></asp:Label>
          </ItemTemplate>
      </asp:TemplateField>
  </Columns>
</asp:GridView>

我正在尝试类似的操作

 protected void btnSave_Click(object sender, EventArgs e)
{
    if (btnSave.Text == "Save")
    {
        for (int i = 0; i < GvCategoryLive.Rows.Count; i++)
        {
            System.Threading.Thread.Sleep(9000);
            GvCategoryLive.Rows[i].Cells[2].Text = "Success";

        }
    }

}

3 个答案:

答案 0 :(得分:1)

我真的不记得gridviews和asp.net Webforms了,但是

您可以在javascript中使用setTimeout

var rowCount = 100;// get row count here
for(int i = 0; i < rowCount ; i++)
{
   setTimeout($('#cell'+i).html('Success'), 9000);
}

答案 1 :(得分:1)

您可以尝试这样:

protected void btnSave_Click(object sender, EventArgs e)
{
    if (btnSave.Text == "Save")
    {
        foreach(GridViewRow row in GvCategoryLive.Rows) {
                if(row.RowType == DataControlRowType.DataRow) {
                    Label UnitMesurement_Id= row.FindControl("UnitMesurement_Id") as Label ;
                    UnitMesurement_Id.Text = "Success";
                }
        }
    }       
}

答案 2 :(得分:0)

因为您没有设置单元格的值,而是在该单元格内设置了标签,所以您应该更改此-

GvCategoryLive.Rows[i].Cells[2].Text = "Success";

进入-

Label UnitMesurement_Id = GvCategoryLive.Rows[i].FindControl("UnitMesurement_Id") as Label;
UnitMesurement_Id.Text = "Success";