现在我要做的是 只要点击 gridview 中的某一项,就可以按这个 项 进行查找
并把结果显示在原来的 gridview 中
  原来准备用gridview 中的 bottonfield 这个的,可是用了以后发现 获取不了
它的值(就是在gridview 中显示的值)

解决方案 »

  1.   

    你可以不用获取button的啊,获取该行其它的cell的值也可以啊,或者用checkbox的。
    要不你就button写个事件传一个值。
      

  2.   

    将你要获取的列名放进DataKeyNames属性,用commandfiled中的[选择].在GridView1_SelectedIndexChanging事件中写获取
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Response.Write(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
        }
      

  3.   


    <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
             <asp:ButtonField ButtonType="Button"  CommandName="Query" />
    </Columns>
            </asp:GridView>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Query")
            {
                Response.Write(e.CommandArgument.ToString());
            }
        }
      

  4.   

    绑顶的时候设置DataKeyNames 属性this.GridView1.DataKeyNames = new string[] { "key" };key 为主键,或者是你准备把它设为主键protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Query")
            {
                Response.Write(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            }
        }
    会在页面输出当前选中行的key 剩下的工作 你根据你的需求自己改下吧~
    有疑问请联系~
      

  5.   

    给你一实例吧  -- 实例之点击获得某一单元格,获得单元格的值获得的值你要作何操作那是你以后的事了,代码帖上即可  demo_gridviewexp文件名前台<%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo_gridviewexp.aspx.cs" Inherits="codeexample_demo_gridviewexp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        实例之占击获得某一单元格的值
            <asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound">
            </asp:GridView>
        
        </div>
        </form>
        <script type="text/javascript">
        var isIe=window.navigator.appName.indexOf("Netscape") == -1?true:false;
        function onCellClick(srcCell){
            var sCellText=isIe?srcCell.innerText:srcCell.textContent;
            alert(sCellText);
        }
        function initCellClick(){
            var cells=document.getElementById("<%=GridView1.ClientID %>").getElementsByTagName("td");
            for(var i=0;i<cells.length;++i){
                cells[i].onclick="onCellClick(this)";
            }
        }
        //initCellClick();
        </script>
    </body>
    </html>后台using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class codeexample_demo_gridviewexp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = GenerateTable();
            GridView1.DataBind();
        }    private DataTable GenerateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            DataRow row;
            Random rnd = new Random();
            for (int i = 1; i != 31; ++i)
            {
                row = dt.NewRow();
                row[0] = i;
                row[1] = "Product_" + i;
                dt.Rows.Add(row);
            }        return dt;
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            GridViewRowCollection rows = GridView1.Rows;
            TableCellCollection cells;
            for (int i = 0; i != rows.Count; ++i)
            {
                cells = rows[i].Cells;
                for (int j = 0; j != cells.Count; ++j)
                {
                    cells[j].Attributes.Add("onclick", "onCellClick(this)");
                }
            }
        }
    }
      

  6.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body >
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
            <Columns>
                     <asp:ButtonField ButtonType="Button" Text="查询"  CommandName="Query" />
            </Columns>
                    </asp:GridView>
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Default5 : System.Web.UI.Page
    {
        ICollection CreateDataSource()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            System.Data.DataRow dr;
            dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
            dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
            dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));        for (int i = 0; i < 8; i++)
            {
                System.Random rd = new System.Random(Environment.TickCount * i); ;
                dr = dt.NewRow();
                dr[0] = "班级" + i.ToString();
                dr[1] = "学生" + i.ToString();
                dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
                dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
                dt.Rows.Add(dr);
            }
            System.Random newRd = new System.Random(Environment.TickCount * 0); ;
            dr = dt.NewRow();
            dr[0] = "班级0" ;
            dr[1] = "学生0" ;
            dr[2] = System.Math.Round(newRd.NextDouble() * 100, 2);
            dr[3] = System.Math.Round(newRd.NextDouble() * 100, 2);
            dr[4] = System.Math.Round(newRd.NextDouble() * 100, 2);
            dr[5] = System.Math.Round(newRd.NextDouble() * 100, 2);
            dt.Rows.Add(dr);
            System.Data.DataView dv = new System.Data.DataView(dt);
            return dv;
        }    protected void Page_Load(object sender, EventArgs e)
        {
            this.GridView1.DataSource = CreateDataSource();
            this.GridView1.DataKeyNames = new string[] { "学生姓名" };
            this.GridView1.DataBind();
            
        }    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Query")
            {
                DataView dv=(DataView)GridView1.DataSource;
                dv.Sort = "学生姓名";            GridView1.DataSource = dv.FindRows(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() );
                GridView1.DataBind();
            }
        }}