请问各位大侠,在RowCommand里面怎么取gridview主键的值,如在vs2003里面 this.datagrid.datakey[e.item.itemindex].tostring 可以取到,但在VS2005里面怎么不行呢?该怎么在RowCommand事件中取主键,谢谢!

解决方案 »

  1.   

    this.GridView.DataKeys[e.NewSelectIndex].ToString()
    事件(object e , GridViewSelectEventArgs)
      

  2.   

    上面的朋友说的事件GridViewSelectEventArgs我这里怎么没有啊?
    我用this.GridView.DataKeys[e.NewSelectIndex].ToString() 在rowcommand里面取主键还是不行,因为e.下面没有NewSelectIndex,我是在GridView中添加了一个按钮列,并为按钮commandname命了名字,我想通过点击该按钮取得GridView列中主键ID的值,难道不是在rowcommand事件里面取吗?我用VS2003很熟,但现在刚用VS2005就遇到这样的问题?望各位朋友指点一下?谢谢!
      

  3.   

    GridView1.SelectedDataKey.Value.ToString();//主键GridView1_SelectedIndexChanged(object sender, EventArgs e)事件试试
    按钮的commandname是“select”,也可以实现删除编辑等功能
      

  4.   

    请参考下面的code而改你就相应的WebControl(下面的code中是LinkButton):  int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                string pk = gvCompany.DataKeys[rowIndex].Value.ToString();
      

  5.   

     protected void pro_GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
             
            if (e.CommandName == "delbt")
            {
                string proid = this.pro_GridView1.SelectedDataKey.Value.ToString();//提示出错/这里该怎么取主键的值
                Response.Write(proid);        }
        }我在GridView绑定的时候是用 this.pro_GridView1.DataKeyNames= new string[] { "pj_id"};  是不是我主键绑定错误啊上面buller提的方法在SelectedIndexChanged事件里面没有commandname,所以也不行。
      

  6.   

    在GridView声明语句里设置主键.
      

  7.   

    直接将主键绑定在按钮的commandargument上
      protected   void   pro_GridView1_RowCommand(object   sender,   GridViewCommandEventArgs   e) 
            { 
                      
                    if   (e.CommandName   ==   "delbt") 
                    { 
                            string   str=e.CommandArgument();
                    } 
            } 试试
      

  8.   

    在GridView声明语句里设置主键.
    DataKeyname属性中设置主键
    在<asp  gview
    ....
    /gview>声明语句里设置.
      

  9.   


    <asp:TemplateField>
           <ItemTemplate>
               <asp:LinkButton runat="server" ID="lbJoin" CommandName="joinSchool" Text="加入"    CommandArgument='<%#DataBinder.Eval(Container,"RowIndex") %>' OnCommand="lbJoin_Command" CausesValidation="false"></asp:LinkButton>
           </ItemTemplate>
                        </asp:TemplateField> 后台:    protected void lbJoin_Command(object sender,CommandEventArgs e)
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            int sch_id = Convert.ToInt32(SchoolView.DataKeys[rowIndex].Value);
            if (Users.SetSchoolId(Convert.ToInt32(Session["UserID"]), sch_id))
            {
                Session["S_ID"] = sch_id;
                Response.Redirect("FindClass.aspx");
            }
            else
            {
                insertHint.InnerHtml="<font color='red'>加入学校失败.请重试..</font>";
            }
        }
      

  10.   

     
    protected   void   pro_GridView1_RowCommand(object   sender,   GridViewCommandEventArgs   e) 
            { 
                      
                 string ID=this.pro_GridView1.DataKeys[e.rowindex]["列名"].value.tostring();
            } 

    上面也应该可以把
      

  11.   

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "select")
            {
                string id = e.CommandArgument.ToString();
            }
        }
    就是这样!
      

  12.   

    11楼所说的string ID=this.pro_GridView1.DataKeys[e.rowindex]["列名"].value.tostring();
    那红色字体的地方在rowcommand里面没有rowindex方法
    在RowEditing,RowDeleting,RowUpdating这几个事件里面很容易取到主键的
    用string proid = this.pro_GridView1.DataKeys[e.RowIndex].Value.ToString();就可以取到了,但在RowCommand事件里面这样取不行但7楼的方法是一种可以取到的,不知道各位还有上面其它的好方法呢?