如何使用RowDataBound向GridView添加数字顺序?

时间:2011-12-26 05:22:45

标签: c# asp.net

我是一名新的ASP.NET开发人员,我试图通过给它一个从1开始的数字来改变GridVie中每行中第一个单元格的值。这意味着我想将顺序添加到列表中将在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;
        }
    }

2 个答案:

答案 0 :(得分: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++;
        }
    }

答案 1 :(得分:0)

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

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