我现在想在GridView上点击某一行,然后获得这一行各字段的值,该怎么写?
有GridView.Row[]这样的吗?

解决方案 »

  1.   

    DBGrid 有个属性叫做SelectRows,他是一个集合,包含所有被选中的行
    你可以通过DataGridView1.SelectRows[0]来访问第一行 
      

  2.   

     后台: GridView1_RowDataBound事件: if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#FFC729',this.style.fontWeight='';");                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
                        e.Row.Attributes.Add("ondblclick", "rekey('" + e.Row.Cells[1].Text + "','" + e.Row.Cells[10].Text + "')");
                        e.Row.Attributes["style"] = "Cursor:hand";
                    }
    前台:
    <script language="javascript" type="text/javascript">
    function rekey(name,cdid)
    {window.opener.document.getElementById("Text_PD_Name").value=name;
    window.opener.document.getElementById("HiddenCDID").value=cdid
    window.close();
    }
    </script>
      

  3.   

    dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();或者
    dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
      

  4.   

    GridView有两种方式可以取到值,一种是通过Cells[index].Text得到表格显示的字符串的值。
    另一种是通过设置DataKeyNames属性为要获取值的列的名称,然后通DataKeys[index/name].Value/Values[index]得到数据的原值。
      

  5.   

    请问一下,那个Cells[index].Text中的index是什么?索引?
      

  6.   

    再顶顶,实在还是不太懂,
    我写了:
    DataKeyNames="pan"
    GridView.DataKeys["pan"].Value,系统说不行,
    有个简单的例子吗?
      

  7.   

    GridView.DataKeys["pan"].cell[第几列].text
      

  8.   

    <asp:GridView ID="grdView" runat="server" AutoGenerateColumns="False" DataKeyNames="fID" OnRowDataBound="grdView_RowDataBound">
    ...
    </asp:GridView>
    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType != DataControlRowType.DataRow)
    {
    return;
    }
    object objValue = this.grdBooks.DataKeys[e.Row.RowIndex].Value;
    if (objValue != null)
    {
    //objValue为所取的值
    }
    }
      

  9.   

    如下:
    <asp:GridView   ID="grdView"   runat="server"   AutoGenerateColumns="False"   DataKeyNames="fID"   OnRowDataBound="grdView_RowDataBound"> 
    ... 
    </asp:GridView> 
    protected   void   grdView_RowDataBound(object   sender,   GridViewRowEventArgs   e) 

        if   (e.Row.RowType   !=   DataControlRowType.DataRow) 
        { 
            return; 
        } 
        object   objValue   =   this.grdView.DataKeys[e.Row.RowIndex].Value; 
        //object   objValue   =   this.grdView.DataKeys[e.Row.RowIndex].Values[0]; 
        if   (objValue   !=   null) 
        { 
            //objValue为所取的值 
        } 

      

  10.   

    还是要在麻烦问一下,即如我每行有两个字段A和B,我设置了:
    DataKeyNames="A,B",
    那我该怎么分别获取这两个字段的值?我写了如下:
    grdView.DataKeys[0].Values;
    grdView.DataKeys[1].Values;
    系统说“索引超出范围。必须为非负值并小于集合大小。参数名: index”,
    还是很糊涂.... 
      
      

  11.   

    如下:
    grdView.DataKeys[rowIndex].Values[0]; 
    grdView.DataKeys[rowIndex].Values[1]; 
      

  12.   

    或如下: 
    grdView.DataKeys[rowIndex].Values[colName0];   
    grdView.DataKeys[rowIndex].Values[colName1];   
      

  13.   

    或如下: 
    grdView.DataKeys[rowIndex].Values[colName0];   
    grdView.DataKeys[rowIndex].Values[colName1];   
      

  14.   

    那个rowIndex是个什么,系统默认的当前所选行的索引值?
      

  15.   

    我想你对那个gridview怎么存储的有点迷惑,gridview显示其实就是一个二维数组,有行和列,
    grdView.DataKeys[rowIndex].Values[colName0];       
    grdView.DataKeys[rowIndex].Values[colName1];  
    这里的rowIndex就是你要获取哪一行的值,比如你现在选取第二行,它的值就应该是1(下标从0开始的),colName0,colName1
    就是列的字段名,比如你有“name” “age”两个字段,那么colName0的值就应该是name
    至于rowindex的值如何获得,就看你怎么实现了,最平常就是用gridview里面的select事件,可以获得此时的行号,也就是rowindex,不知道这样讲可明白