在grideview中选中一行怎样获得这一行的编号

解决方案 »

  1.   

    如果是ButtonField, GridView.RowCommand事件处理代码的e.CommandArgument就是行号。
      

  2.   

    e.CommandArgument
    必须要在GridViewCommandEventArgs这个事件的处理函数中才能使用
      

  3.   


    例如点击了当前行是个LinkButton
    protected   void   LinkButton1_Command(object   sender,   CommandEventArgs   e) 

     LinkButton   lb   =   (LinkButton)sender; 
     DataControlFieldCell   dcf   =   (DataControlFieldCell)lb.Parent; 
     GridViewRow   gvr   =   (GridViewRow)dcf.Parent; 
     GridView1.SelectedIndex   =   gvr.RowIndex; 
      

  4.   


    <asp:ButtonField ButtonType="Button" CommandName="Xyz" Text="按钮" />protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if(e.CommandName == "Xyz")
                Response.Write(e.CommandArgument.ToString());
    }
    另外一种情况是你启用了选定内容。
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
            Response.Write(GridView1.SelectedIndex);
    }还有一种情况是TemplateField内的<asp:Button>通过CommandArgument='<%# Container.DisplayIndex %>'传递行索引。取得行索引后再通过GridView1.DataKeys[行索引].Value.ToString()来取得对应的主关键字段值。
      

  5.   

    int index = Convert.ToInt32(e.CommandArgument);
    如果使用模板列的话,index = null,你需要在RowCreated对他赋值:
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
    LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
    }}
      

  6.   

    模板列添加Button 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Button btn = (Button)e.Row.FindControl("Button9");
            if (btn != null)
            {
                btn.CommandArgument = e.Row.RowIndex.ToString();
            }
        }protected void Button9_Command1(object sender, CommandEventArgs e)
        {
            Response.Write(e.CommandArgument.ToString());
        }
      

  7.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
             if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
            {
                e.Row.Attributes.Add("id",(e.Row.RowIndex+1).ToString());//这里可不写            e.Row.Attributes.Add("onclick","alert('你当前选择的是第'+this.id+'行!')");
                //或者
                 //e.Row.Attributes.Add("onclick","alert('您当前选择的是第"+(e.Row.RowIndex+1)+"行!')");
              
          // 或者
            CheckBox check = e.Row.FindControl("checkbox1") as CheckBox;
                if (check != null)
                {
                    check.Attributes.Add("onclick","alert('你当前选择的是第"+(e.Row.RowIndex+1)+"行')");
                }   
              }     
        }
     <asp:GridView ID="GridView1" Font-Size="9pt" runat="server"  HorizontalAlign="Center" OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
    <Columns>
                             
                       <asp:TemplateField><ItemTemplate><asp:CheckBox ID="checkbox1" runat="server" /></ItemTemplate></asp:TemplateField>
                    </Columns>
                        </asp:GridView>
      

  8.   

    我是要获得这一行数据的编号,部是获得grideView的编号
      

  9.   

    什么叫作数据的编号啊?GridView的DataKeyNames="你所谓的编号"的话,那么取得了行号后GridView.DataKeys[行号].Value不就是你所谓的编号吗?
      

  10.   


    <asp:GridView ID="GridView2" runat="server">
            <Columns><asp:BoundField DataField="id" /></Columns>
            </asp:GridView>
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
            {
                 e.Row.Attributes.Add("onclick","alert('当前行的数据id是"+e.Row.Cells[0].Text+"')");
            }
      

  11.   


    int index1;
    string tempsid = "";
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
      if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
      {
              index1 = GridView1.Rows[i].RowIndex;          tempsid = GridView1.DataKeys[index1]["sid"].ToString();//sid为此行数据在数据库里的主键号
      }
    }tempsid就是你要的
      

  12.   

    设置DataKeys 属性然后
    CustomersGridView.DataKeys[index].Value.ToString();//index为当前行的索引值
      

  13.   

    int index = Convert.ToInt32(e.CommandArgument);
      

  14.   

    int index = Convert.ToInt32(e.CommandArgument);
      

  15.   

    后台代码:
    你可以使用sqlhelper,本文没用。代码如下:
    using System;
    using System.Data;
    using System.Configuration;
    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;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {//清清月儿http://blog.csdn.net/21aspnet 
        SqlConnection sqlcon;
        SqlCommand sqlcom;
        string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }//删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr,sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            bind();
        }//更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strCon);
            string sqlstr = "update 表 set 字段1='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='" 
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcom=new SqlCommand(sqlstr,sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }//取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }//绑定
        public void bind()
        {
            string sqlstr = "select * from 表";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "表");
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "id" };//主键
            GridView1.DataBind();
            sqlcon.Close();
        }
    }前台主要代码:
                                ... ...
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                            ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                            OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
                                <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                                <asp:BoundField DataField="员工性别" HeaderText="性别" />
                                <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                                <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                                <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                            </Columns>
                            <RowStyle ForeColor="#000066" />
                            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
      

  16.   

    在girdview上绑数据的id,用id识别多方便啊