HTML: 
<script type="text/javascript"> 
    var oldrow=null; 
    var currentcolor=null; 
    var oldcolor=null; 
    function selectx(row) 
    { 
        if(oldrow!=null) 
        { 
            oldrow.style.backgroundColor=oldcolor; 
        } 
        row.style.backgroundColor='#99ccff'; 
        oldrow=row; 
        oldcolor = currentcolor; 
    } 
</script> .cs: 
  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    {         if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            e.Row.Attributes.Add("onmouseover", "if(this!=oldrow){currentcolor=this.style.backgroundColor;this.style.backgroundColor='PeachPuff',this.style.fontWeight='';}"); 
            e.Row.Attributes.Add("onmouseout", "if(this!=oldrow){this.style.backgroundColor=currentcolor;this.style.fontWeight='';}"); 
            e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)"); 
        } 
    }用了这种方法可以单击后改变该列的颜色。另外我还由一个delete按钮。此时如果我单击,提示是否是否删除。然后在后台连接数据库,删除制定的行,请问如何在后台。单击delete按钮时。获得选中行的某个字段值呢,比如id。  谢谢如何改这段代码才行呢?

解决方案 »

  1.   

    int   id=   (int)   GridView1.DataKeys[e.ItemIndex].ToString()   
      

  2.   

    2楼的好像不行哦,错误 2 “System.EventArgs”不包含“ItemIndex”的定义,并且找不到可接受类型为“System.EventArgs”的第一个参数的扩展方法“ItemIndex”(是否缺少 using 指令或程序集引用?)
      

  3.   

    将ID添加到gridview的datakeysname,
    再用二楼的来绑定。
    绑定方法要灵活,并不是说二楼的一定能找得到ID,得根据具体情况而定。
      

  4.   

    GridView1.Rows[e.RowIndex].Cells[10].Text;
      

  5.   

    给你一段代码,你参考一下:    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow dr = e.Row;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((ImageButton)e.Row.Cells[0].Controls[3]).Attributes.Add("onclick", "check_del('"+e.Row.Cells[1].Text +"')");//删除按钮加提示
                }
            }    }    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string wlbh = GridView1.DataKeys[e.RowIndex].Value.ToString() ; //表中主键        //check for it by used
            bool is_exists=mater_product.wlbh_is_used(wlbh);
            if (is_exists)
            {
                Page.RegisterStartupScript("Duplication","<script>alert('此物料已被后面调用,你的操作已被取消')</script>");
                return;
            }
            else
            {
                mater_product.Delete(wlbh);            bind();
            }
        }
      

  6.   

    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandArgument='<%#Eval("hid")%>'
                              CommandName="delete"   OnClientClick="return confirm('确定要删除吗?删除后不可恢复');">删除</asp:LinkButton>protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                string id = e.CommandArgument.ToString();
                string sql="delete Products where pid="+id+"";
               int i= con.DeleteCommand(sql);
               if (i > 0)
               {
                   Response.Write("<script>alert('删除信息成功!'); </script> ");
                   Bind();
               }
               else
               {
                   Response.Write("<script>alert('删除信息失败'); </script> ");
                   Bind();
               }
                
            }
      

  7.   

    各位..我那删除按钮不是在Gridview里面的...而是在Gridview外的啊..单独的一个按钮。
      

  8.   

    我说的是 此 button 根本不在DataGrid里面内嵌的啊....而是在页面上的一个button啊.我删除 是获取不到..e.Row之类的啊.
      

  9.   

    试试这个,他可以取出你选要的任意单元格的值,运行可以通过的:
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                string value;
                value = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value.ToString().Trim();
                MessageBox.Show(value, "结果");        }        private void Form1_Load(object sender, EventArgs e)
            {
                string connstr = "Persist Security Info=false;Initial Catalog=PROJECT;Server=localhost;Integrated Security=SSPI";
                string comdstr = "select * from users";
                SqlConnection conn = new SqlConnection(connstr );
                SqlCommand comd = new SqlCommand(comdstr ,conn);
                SqlDataAdapter adapt = new SqlDataAdapter();
                adapt.SelectCommand = comd;
                adapt.SelectCommand.Connection = conn;
                DataSet ds = new DataSet();
                conn.Open();
                adapt.Fill(ds,"users");
                dataGridView1.DataSource = ds.Tables["users"];
                dataGridView1.Refresh();
                conn.Close();
            }
      

  10.   

    在gridview 加个复选框
    .....
     <Columns>
       <asp:TemplateField HeaderText="选择">
        <ItemTemplate>
        <asp:CheckBox ID="CB_Select" runat="server" />
      </ItemTemplate>
     </asp:TemplateField>
    ....
    </Columns>
    在girdview 外  
     <asp:Button ID="Btn_Delete" runat="server" Text="删除" OnClick="Btn_Delete_Click" /><br />
    删除后台代码(给你参考):  int selectCount = 0; //要删除的员工总数
            string employeeNos = ""; //保存要删除的员工编号
            string oneEmployeeNo; //保存某行记录的员工编号
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)gr.Cells[0].FindControl("CB_Select");
                if (chk.Checked) //如果要删除该员工
                {
                    oneEmployeeNo = gr.Cells[1].Text;
                    if (0 == selectCount)
                        employeeNos = "'" + oneEmployeeNo + "'";
                    else
                        employeeNos = employeeNos + ",'" + oneEmployeeNo + "'";
                    selectCount++;            }
            }        if (0 == selectCount) //如果用户没有选择员工
                Response.Write("<script>alert('对不起,你没有选择员工!');</script>");
            else
            {
                /*如果选择了员工就执行调用业务层进行该些员工信息的删除*/
                EmployeeLogic employeeLogic = new EmployeeLogic();
                if (!employeeLogic.DeleteEmployeeInfo(employeeNos))
                    Response.Write("<script>alert('" + employeeLogic.ErrMessage + "');location.href='EmployeeManage.aspx';</script>");
                else
                    Response.Write("<script>alert('员工信息删除成功!');location.href='EmployeeManage.aspx';</script>");
            }
      

  11.   


    //获取当前选中行的索引
    int i = this.GridView1.SelectedRow.RowIndex;
    string key=this.GridView1.DataKeys[i].ToString();这样应该可以
      

  12.   

    可以再GridView1里面加一个复选框按钮,获取到ID后在用session,application保存然后在delete按钮事件里面调用