我想在rowcommand 事件中获取该行的datakeys[]值。因为需要获取的值不止一个,不能用e.commandargument绑定,不只应该怎样获取?

解决方案 »

  1.   

    GridView1.SelectedDataKey.Values[1]
    GridView1.SelectedDataKey.Values[0]
      

  2.   

    gridview对象设置datakeyfields没有》
      

  3.   

    gridview对象设置datakeyfields没有》
      

  4.   

    gridview 中我设置了datakeynames属性   DataKeyNames="apply_id,state_id"
    在后台代码中
     protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "view")
        {    }
    }
      

  5.   

    gridview 中我设置了datakeynames属性   DataKeyNames="apply,state"
    在后台代码中
     protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    string apply;
      if(e.CommandName == "view")
      {
        apply = gridview1.SelectedDataKey.Values[0].ToString();
        state = gridview1.SelectedDataKey.Values[1].ToString();
       }
    }
      

  6.   

    GridView1.SelectedDataKey["字段名"]
      

  7.   

    难兄难弟啊,这个问题我研究了好久,最后我是这样做的:
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
            If e.CommandName = "cart" Then
                Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
                Dim label1 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label)
                Dim id As Integer = CType(label1.Text.ToString, Integer)
                Dim label2 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproname"), Label)
                Dim produtname As String = label2.Text.ToString
                Dim price As Decimal = CDec(Me.GridView1.Rows(index).Cells(4).Text.ToString)
                MsgBox(id)
                MsgBox(produtname)
                MsgBox(price)
                Profile.Cart.AddItem(id, produtname, price)
                MsgBox("你已成功将商品加入购物车!")
            Else
                Dim index As Integer = Convert.ToInt32(e.CommandArgument.ToString().Trim())
                Dim label1 As Label = CType(Me.GridView1.Rows(index).FindControl("lblproid"), Label)
                Dim id As Integer = CType(label1.Text.ToString, Integer)
                Profile.Favorites.AddProduct(id)
                MsgBox("放入衣柜")
            End If
        End Sub    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If (e.Row.RowType = DataControlRowType.DataRow) Then
               Dim bt1 As Button = CType(e.Row.FindControl("cart"), Button)
                bt1.CommandArgument = e.Row.RowIndex.ToString
                Dim bt2 As Button = CType(e.Row.FindControl("favorite"), Button)
               bt2.CommandArgument = e.Row.RowIndex.ToString
            End If
        End Sub不知道对你有没有帮助
      

  8.   

    这样的话,要在gridview 中添加几个控件,难道就没有别的办法了吗?
      

  9.   

    int rowIndex = Int32.Parse((String)e.CommandArgument)这样写呢?
      

  10.   

    e.CommandArgument只能绑定一项,我想获取多个键值
      

  11.   

    我用这两句可以取到第二个主键的值,你自己看着改吧:    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = Int32.Parse((String)e.CommandArgument);
            Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
        }
      

  12.   

    那你在e.CommandArgument中绑定的是什么?
      

  13.   

    rowindex是索引号,即取出你点击的那行的"序号"我在测试中将datakeys绑定userid和roleid,最后测试结果可以根据我点击的行显示roleid.应该实现了你的目标了吧,哈哈,给分哦
      

  14.   

    简单的说,你要的就是:GridView1.DataKeys[rowIndex].Values[x];  当然,在这之前,你还要将"索引号"rowIndex的值取出来,方法就是: int rowIndex = Int32.Parse((String)e.CommandArgument);这样够明白了吧
      

  15.   

    朋友,我想知道你的CommandArgument绑定的是什么?语句怎么写的?因为我调试时显示
    ((String)e.CommandArgument为null
      

  16.   

    CommandArgument照资料说是"记录集内被单击行的索引".我不知道你所谓的绑定是什么意思.我的测试环境很简单,没有绑定什么.就一个简单的gridview+datasource,gridview有选择列,只有唯一一个事件就是    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = Int32.Parse((String)e.CommandArgument);
            Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
        }
      

  17.   

    以下是我的测试页面:        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:officeautoConnectionString %>"
                SelectCommand="SELECT [userid], [username], [password], [roleid], [realname] FROM [tusers]">
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="userid,roleid"
                DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" HorizontalAlign="Center" AllowPaging="True" AllowSorting="True">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="userid" HeaderText="userid" InsertVisible="False" ReadOnly="True"
                        SortExpression="userid" />
                    <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
                    <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                    <asp:BoundField DataField="roleid" HeaderText="roleid" SortExpression="roleid" />
                    <asp:BoundField DataField="realname" HeaderText="realname" SortExpression="realname" />
                </Columns>
            </asp:GridView>
      

  18.   

    我在gridview中使用了模板列 可是e.CommandArgument取不到值
    我的后台代码:
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       int rowIndex = Int32.Parse((String)e.CommandArgument);
       Response.Write(GridView1.DataKeys[rowIndex].Values[0]);
       Response.Write(GridView1.DataKeys[rowIndex].Values[1]);
    }
     
    前台的测试页面:
     <asp:GridView ID="gridview1" runat="server" DataKeyNames="id,name" AutoGenerateColumns="False"  OnRowCommand="gridview1_RowCommand" >
    <Columns>
     <asp:TemplateField HeaderText="测试">
      <ItemTemplate>
        <table style="width:540px; height:170px">
         <tr>
           <td align="left" style="height:8px">
             <asp:Label ID="lblId" runat="server"><%#Eval("id") %></asp:Label></td>
         </tr>
         <tr>
           <td>
             <asp:Label ID="lblName" runat="server"><%#Eval("name")%></asp:Label></td>
         </tr>  
         <tr>
           <td>
           <asp:LinkButton ID="lbtnTest" runat="server" CommandName="view">查看</asp:LinkButton></td>
         </tr>
     </table>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
      

  19.   

    你的gridview大小写根本没对应啊,怎么回事,难道没报错?
      

  20.   

    把<asp:LinkButton ID="lbtnTest" runat="server" CommandName="view">查看</asp:LinkButton>去掉.然后在 <Columns>内增加 <asp:CommandField ShowSelectButton="True" />测试通过.
      

  21.   

    但这样一来,命令按钮就和table 中的内容为水平排列,我是希望gridview 中只有一列,命令按钮在table里面单独占以一行。
      

  22.   

    请问:用SQL Server中的select 语句如何得到所查结果集中每一条记录的行索引,也就是说,知道那是第几条记录 ,如果可以得到的话,就用这个索引值mod gridview1.pagesize 来绑定e.commandargument.
      

  23.   

    还是用了wxm0930(赤道与北极)朋友的方法,问题解决,谢谢各位了。
    结帖。