if(((CheckBox)DataGrid1.Items[i].FindControl("GroupId")).Checked==true)

解决方案 »

  1.   

    好像遍历有问题
    我是这样遍历的,你参考一下
    /// <summary>
    /// 求合计
    /// </summary>
    /// <param name="myDG">当前操作的DG</param>
    /// <param name="FristCell">求和的第一个值端</param>
    /// <param name="SecondCell">求和的第一个值端</param>
    /// <param name="mytablename">表的名称</param>
    void myTotalPrice(DataGrid myDG,int FristCell,int SecondCell,string mytablename,string myTotalField)
    {
    Double mymoney=0;
    Double FristValue=0;
    Double SecondValue=0;
    foreach(DataGridItem dgi in myDG.Controls[0].Controls)//.Items)
    {
    if (dgi.ItemType==ListItemType.Item||dgi.ItemType==ListItemType.AlternatingItem)
    {
    FristValue=Convert.ToDouble(dgi.Cells[FristCell].Text);
    if (SecondCell==0)
    {
    SecondValue=1;
    }
    else
    {
    SecondValue=Convert.ToDouble(dgi.Cells[SecondCell].Text);
    }
    mymoney+=FristValue*SecondValue;}
    if (dgi.ItemType==ListItemType.Footer)
    {
    if (mytablename!="T_Q_OP")
    {
    D_L_TotalPrice+=mymoney;//合计价钱
    }
    ((Label)dgi.Cells[myDG.Columns.Count-1].FindControl("total_price")).Text=mymoney.ToString();
    }
    }
    }
      

  2.   

    可能不是这个问题,
    你在page_load中是否绑定了DataGrid,如果是
    就在代码放在
    if(!IsPostBack)
    {
    //databind code
    }
      

  3.   

    for(int i=0;i<DataGrid1.Items.Count;i++)
    {
    CheckBox checkbox=(CheckBox)DataGrid1.Items[i].FindControl("GroupId");
    if (checkbox.Checked)
    {

              } 
    }
      

  4.   

    你为什么要连续两次读取CheckBox的Checked值啊?
    我修改后的代码!
    private void Button2_Click(object sender, System.EventArgs e)
    {
    for(int i=0;i<=DataGrid1.Items.Count;i++)
    {
    CheckBox MyCheck=(CheckBox)DataGrid1.Items[i].Cells[2].FindControl("GroupId");
    if(MyCheck.Checked)
    {
    Label1.Text=MyCheck.Text;
    }
    else
    {
    Label1.Text="0";
    }
    }
    }
      

  5.   

    int j=0;
    foreach(DataGridItem item in AppGrid.Items )
    {
    if(((CheckBox)item.FindControl("CheckBox2")).Checked)
    //proccess code
    //if(((CheckBox)item.Cells[3].FindControl("CheckBox2")).Checked)
    j++;
    }
      

  6.   

    allanyan(allan) :还是不行阿,label1得值始终是0,怎么选他也不为1
      

  7.   

    总结大家的经验:1.page_load中要加上if(!ispostback)
                    2.应用allanyan(allan) 的写法。
    现在终于有了一点成果,那就是,只有选择最后一行的时候label1得值才有显示,而选择其他的没有反应,这又是什么毛病???
      

  8.   

    参考一下
    private void btnUpdate_Click(object sender, System.EventArgs e)
    {
      // Loop through the items in the datagrid.
      foreach (DataGridItem di in dgContacts.Items)
      {
        // Make sure this is an item and not the header or footer.
        if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem)
        {
          // Get the current row for update or delete operations later.
          DataRow dr = _dsContacts.Tables["Contact"].Rows.Find(dgContacts.DataKeys[di.ItemIndex]);      // See if this one needs to be deleted.
          if (((CheckBox)di.FindControl("chkDelete")).Checked)
          {
            _dsContacts.Tables["Contact"].Rows.Remove(dr);
          }
          else
          {
            // Update the row instead.
            dr["Email"] = ((TextBox)di.FindControl("Email")).Text;
            dr["FirstName"] = ((TextBox)di.FindControl("First")).Text;
            dr["LastName"] = ((TextBox)di.FindControl("Last")).Text;
          }
        }
      }   // Save the changes if there are any.
       if (_dsContacts.HasChanges())
      {
        SaveContacts();
      }  BindContacts();
    }
      

  9.   

    不对不对,为什么加上
    DelId="delete groups where groupid=" + MyCheck.Text.ToString();
    MyMcgsClass.Mcgs_DataConnOpen("../bbsdb.mdb");
    OleDbCommand Mcgs_GroupDel=new OleDbCommand(DelId,MyMcgsClass.Mcgs_OleDbConn);
    Mcgs_GroupDel.ExecuteNonQuery();
    又删不掉数据了