2011-12-26 76 views
0

我是一个新的ASP.NET开发人员,我试图通过给它一个从1开始的数字来改变GridVie中每一行的第一个单元格的值。这意味着我想添加命令到事物列表这将显示在GridView中。如何使用RowDataBound将数字顺序添加到GridView?

我正在使用GridView的RowDataBound方法,但现在我不知道要在其中设置for循环的限制。任何人都可以帮助我吗?

我的后台代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) { 
      for(int i=1, i<; i++) 
       e.Row.Cells[0].Text = i; 
     } 
    } 
+0

尝试获取gridview行数并检查我 prema 2011-12-26 05:34:21

回答

1

如果你的要求是“在每个更改第一个单元格的值行“,它会更像︰

private int _rowIndex=0; 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) { 
       e.Row.Cells[0].Text = _rowIndex.ToString(); 
       _rowIndex++; 
     } 
    } 
0

你可以试试这个,它会返回细胞计数:

int cells = ((TableRow) (e.Row)).Cells.Count;