获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段 <asp:TemplateField HeaderText=> <asp:LinkButton ID= runat= CommandName= Eval("Id") %>'>签入</asp:LinkButton> <asp:LinkButton ID= runat= CommandName=>签出</asp:LinkButton> </asp:TemplateField> 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引 gv_Company_RowCommand(sender, GridViewCommandEventArgs e){ (e.CommandName == ) GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); id = Convert.ToInt32(e.CommandArgument.ToString()); index=e.CommandArgument.ToString(); id=Convert.ToInt32(GridView1.Rows[Convert.ToInt32(index)].Cells[0].Text); } } <ItemTemplate> ID= runat= CommandArgument=<%# Eval("field1") %>' CommandName= Text=<%# Eval("field2") %>'> </asp:LinkButton> CommandArgument绑定了字段1 那么, GridView1_RowCommand(sender, GridViewCommandEventArgs e) { ) { lb = (LinkButton)e.CommandSource; a = lb.Text; b = e.CommandArgument; } 如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下: <asp:TemplateField> <ItemTemplate> <asp:Button runat= CommandArgument=Eval("id") %>' Text= /> </ItemTemplate> </asp:TemplateField> 一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作 如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下: Button btn = (Button)e.Row.FindControl(); (btn != { = e.Row.RowIndex.ToString(); } rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了