Gridview中的RowCommand事件中如何获得该列的行索引?

1.先加个ButtonField,转为模板,把CommandName改下名字成 chooseModel
2.模板列里放的控件的   CommandArgument 属性设置下值 ,没设置的话好像取不到
3.前台代码(部分省略)
CommandArgument ="<%#  GridView1.Rows.Count %>"
4.后台代码
protected void GridViewIw1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
        if (e.CommandName == "chooseModel")
        {
            int index = Convert.ToInt32(e.CommandArgument); //行号,从上到下0,1,2,3,4
            string CID = GridViewIw1.DataKeys[index].Value.ToString();  //从行号中获得主键的值                
            Label lb = (Label)GridView2.Rows[rowIndex].FindControl("Label1"); //或行号中获得该列的值
            Response.Write(lb.Text.ToString());
        }
  }

你可能感兴趣的:(Gridview中的RowCommand事件中如何获得该列的行索引?)