从页脚模板中的链接按钮获取行索引

时间:2014-02-28 13:54:32

标签: c# asp.net gridview

我对Gridview的这个问题感到沮丧。

如果点击页脚行中的链接按钮,我想在gridview的 RowCommand 事件中获取行索引。

我总是在index

中得到-1作为值
        Control ctl = e.CommandSource as Control;
        GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
        int index = CurrentRow.RowIndex;

如何摆脱这个问题?

1 个答案:

答案 0 :(得分:1)

据我所知,页脚行将始终具有RowIndex = -1。我试过做一些测试,你也可以尝试一下,对于DataRow类型的每一行,我们都有一个RowIndex,但对于FooterRow类的行,我们总是有-1值。下面是我的测试代码(仅供参考,不需要一些代码行)。

protected void grdGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
            int index;

            foreach (GridViewRow row in grdGrid.Rows)
            {
                if (CurrentRow.RowType == DataControlRowType.DataRow)
                {
                    //you'll have an RowIndex for each DataRow: 0,1,2 and so on
                    index = CurrentRow.RowIndex;
                }
                else if (CurrentRow.RowType == DataControlRowType.Footer)
                    {
                        //the RowIndex will be always -1 
                        index = CurrentRow.RowIndex;
                    }

            }
        }

我希望这会对你有所帮助。