以下代码,是通过遍历GridViewRow中的checked来确定选中的数据行,是否要被删除。
·但是我却不知道该怎么获得行的ID了?请高手指点!
------
protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
            if (chk.Checked)
            {
                string id =???
            }
        }
    }

解决方案 »

  1.   

    http://www.cnblogs.com/weekzero/archive/2006/05/16/401231.aspx我这篇文章中有里边是string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();可以改为string id = GridView1.DataKeys[你的行index].Values[0].ToString();
      

  2.   

    这样你就不能用foreach 了,直接用for吧for (int i=0;i<GridView1.Rows.Count;i++)
            {
                CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
                if (chk.Checked)
                {
                    string id =GridView1.DataKeys[i].Values[0].ToString();
                }
            }
    这样就差不多了
      

  3.   

    ·不对,你获得ID的事件是在GridView自带的事件,但是我的是Button中的事件,不一样的!!!
    所以用e.RowIndex是不行的!
      

  4.   

    ·是不是用foreach不能做到呢,其实有些时候也是需要这种处理状态的!
      

  5.   

    为何不设置CheckBox的value为关键字段值?
      

  6.   

    for (int i=0;i<GridView1.Rows.Count;i++)
            {
                CheckBox chk = (CheckBox)gr.Cells[6].FindControl("itemchk");
                if (chk.Checked)
                {
                    string id =GridView1.DataKeys[i].Values[0].ToString();
                }
            }对了
      

  7.   

    ·CheckBox的Value只有True/False!!!
      

  8.   

    ·是不是用foreach不能做到呢,其实有些时候也是需要这种处理状态的!
      

  9.   

    ·根据前两位的提示,我把关于基于数据控件对象的Rows/column弄了个明白,其实非常简单,我以将两种方法分别实现了,如下:
    ---基于foreach的---
     foreach (GridViewRow GR in GridView1.Rows)
            {            
                CheckBox chk0 = (CheckBox)GR.Cells[6].FindControl("itemchk");
                if (chk0.Checked)
                {
                    string id = GridView1.DataKeys[GR.RowIndex].Values[0].ToString();
                    string sqlstr = "delete from authors where au_id='" + id + "'";                //以下数据操作可以封装到全局类
                    string constr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
                    SqlConnection sqlCon = new SqlConnection(constr);
                    sqlCon.Open();
                    SqlCommand sqlCom = new SqlCommand(sqlstr, sqlCon);
                    sqlCom.ExecuteNonQuery();
                }
            }
    ---基于FOR的---
    for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[6].FindControl("itemchk");
                if (chk.Checked)
                {
                    string id = GridView1.DataKeys[i].Values[0].ToString();
                    string sqlstr = "delete from authors where au_id='" + id + "'";                string constr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
                    SqlConnection sqlCon = new SqlConnection(constr);
                    sqlCon.Open();
                    SqlCommand sqlCom = new SqlCommand(sqlstr, sqlCon);
                    sqlCom.ExecuteNonQuery();
                }
            }