我想实现单击GridView内的记录时,使被点击的单元格获得焦点,怎么实现?
也就是:如何获取GridView被单的单元格的行索引和列索引?请各位指教,谢谢!

解决方案 »

  1.   

    这个东西你需要diy了,你可以利用一个hidden来保存信息,然后给每个单元格加上点击的attribute,需要了解js
      

  2.   

    <asp:TemplateField HeaderText="编号">
                                                                            <ItemTemplate>
                                                                                <asp:Label ID="Label1" runat="server" Text="<%# Container.DataItemIndex%> "></asp:Label>
                                                                            </ItemTemplate>
                                                                        </asp:TemplateField>
    在一个模版里绑定列
    protected void imgBtnEdit_Command(object sender, CommandEventArgs e)//我用的是imagebutton,你可以用别的,但事件事command事件
    {
    int index = Convert.ToInt32(e.CommandArgument);//获取行号
                GridViewRow row = GridView1.Rows[index];//获取该行
                stirng s=row.Cells[2].Text;//该行第二列数据
    }
      

  3.   

    先在页面上放两个文本框和一个按钮,给他们添加客户端属性 style="display:none" 也就是不显示。
    给每个单元格,添加客户端onclick事件,在该事件处理函数中把该单元格所在行号和列号付给两个方本框,然后再触发那个按钮的回发事件就行了。
      

  4.   

    我没用过VS2005,所以GridView也不会,但我想应该和DataGrid差不多吧,下面是我用VS2003的DataGrid写的例子,侧试通过,至于有没有更好的方法,我就不清楚了。页面:clickCell.aspx<%@ Page language="c#" Codebehind="clickCell.aspx.cs" AutoEventWireup="false" Inherits="WebTest.clickCell" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>clickCell</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" runat="server" Width="288px" Height="112px"></asp:DataGrid></FONT>
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
    <asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
    </form>
    </body>
    </HTML>后台编码文件:clickCell.aspx.csusing System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Text;namespace WebTest
    {
    /// <summary>
    /// clickCell 的摘要说明。
    /// </summary>
    public class clickCell : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.LinkButton LinkButton1;
    private   SqlConnection myConnection;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    myConnection=new SqlConnection("server=127.0.0.1;;database=pubs;uid=sa;pwd=sa;");
    this.TextBox1.Attributes.Add("style","display:none");
    this.TextBox2.Attributes.Add("style","display:none");
    this.LinkButton1.Attributes.Add("style","display:none");
    BuildJs();
    if(!this.Page.IsPostBack)
    {
    SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors",myConnection);
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "Authors");
    DataGrid1.DataSource=ds.Tables["Authors"].DefaultView;
    DataGrid1.DataBind();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    this.DataGrid1.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound); }
    #endregion
    private void LinkButton1_Click(object sender, System.EventArgs e)
    {
    Response.Write("行号:" + TextBox1.Text + ";列号:" + TextBox2.Text + "。");
    } private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType == ListItemType.Item)
    {
    for(int i=0;i<e.Item.Cells.Count;i++)
    {
    e.Item.Cells[i].Attributes.Add("onclick","javascript:clickCell('" + e.Item.ItemIndex + "','" + i + "')");
    e.Item.Cells[i].Attributes.Add("style","cursor:hand");
    }
    }
    } private void BuildJs()
    {
    StringBuilder js = new StringBuilder();
    js.Append("<script language=javascript>" + Environment.NewLine);
    js.Append("    function clickCell(rowIndex,cellIndex)" + Environment.NewLine);
    js.Append("    {" + Environment.NewLine);
    js.Append("        document.getElementById('"+ TextBox1.ClientID +"').value=rowIndex;" + Environment.NewLine);
    js.Append("        document.getElementById('"+ TextBox2.ClientID +"').value=cellIndex;" + Environment.NewLine);
    js.Append("        __doPostBack('" + LinkButton1.UniqueID + "','');" + Environment.NewLine);
    //如果你用的是用户控件,不知道LinkButton1.UniqueID这里要不要进行处理一下,你自己测一下,替换一下应该就行了,以前写过,记不清了,我这个没有写在控件里。
    js.Append("    }" + Environment.NewLine);
    js.Append("</script>" + Environment.NewLine); this.Page.RegisterClientScriptBlock("clickCell",js.ToString());

    }
    }
    }