protected void Button1_Click(object sender, EventArgs e) 
    { 
        
        for (int i = GridView1.Rows.Count-1;i>0 i--) 
        { 
            
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2"); 
          // Response.Write(cb.Checked); 
            if (cb.Checked == true) 
            { 
              
                int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);                //  string sql = "delete from Staff where ST_ID="+id; 
                //  Response.Write(sql); 
              //  webService.ExcuteSql(sql); 
          } 
        }     } 

解决方案 »

  1.   

    int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);    
    ------------------
    int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value[n]);
    n为列号或者列名    
      

  2.   

    int id = Convert.ToInt32(this.GridView1.DataKeys[i].Values[n]); 
      

  3.   


    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
                AutoGenerateColumns="False"  Width="100%" CellPadding="0" 
            BorderStyle="None" OnRowDataBound="GridView1_RowDataBound" PageSize="2" 
            onpageindexchanging="GridView1_PageIndexChanging" DataKeys="ST_ID"> 
      

  4.   

    Datakeys 设置拉问题还是有.还有提示出没有datakeys这个属性啊??我加上了可还是这个错
      

  5.   

    int id = Convert.ToInt32(this.GridView1.DataKeys[GridView1.Items[i].ItemIndex].Value); 
      

  6.   

     protected void Button1_Click(object sender, EventArgs e) 
        { 
            
          
            foreach (GridViewRow di in GridView1.Rows)
            {
                if (((CheckBox)(di.Cells[0].FindControl("CheckBox2"))).Checked == true)
                {
                    int id = Convert.ToInt32(this.GridView1.DataKeys[di.RowIndex].Value);           
                }
            }    }