private void com_cp_SelectedIndexChanged_1(object sender, EventArgs e)//获得零件数
         {
            //string count="";             try
             {
                 this.dataGridView2.Columns.Clear();
                 this.com_bh.DataSource = DCC.SqlDC.SDC.GetTable("select m_ID,m_NO from material where m_ID=" + com_cp.SelectedValue + "");
                 com_bh.DisplayMember = "m_NO";
                 com_bh.ValueMember = "m_ID";
                 this.dataGridView2.DataSource = DCC.SqlDC.SDC.GetTable("select m_ID,dbo.GetMaterialNameAll(m_ID) as '物料成品',单机用量 from material where m_NO like '" + com_bh.Text + "%'");
                 DataGridViewColumn col = new DataGridViewCheckBoxColumn();
                 //this.dataGridView2.Columns.RemoveAt(3);
                 this.dataGridView2.Columns.Insert(3, col);
                 this.dataGridView2.DataSource = DCC.SqlDC.SDC.GetTable("select m_ID,dbo.GetMaterialNameAll(m_ID) as '物料成品',(单机用量*" + Int32.Parse(txt_count.Text) + ") as '单机用量' from material where m_NO like '" + com_bh.Text + "%'");             }
             catch (Exception ex)
             {
                 return;
             }
  }//我在DATAGIRDVIEW查到的第3列添加了CHECKBOX private void btn_tj_Click(object sender, EventArgs e)//批量添加
        {
            try
            {
                int count = 0; 
                btn_bc.Visible = false;
                if (txt_count.Text != "" && txt_bz.Text != "")
                {
                    for (int i = 0; i < dataGridView2.Rows.Count; i++)
                    {
                        if (dataGridView2.Rows[i].Cells[3].EditedFormattedValue.ToString() == "True")    
                        {
                            count++;
                            dataGridView2.EndEdit(); 
                        }
                    }
                    if (count == 0)
                    {
                        MessageBox.Show("请至少选择一条数据!", "提示");
                        return;
                    }                    else
                    {
                        for (int i = 0; i < count; i++)
                        {
                            label2.Text = DateTime.Now.ToString();
                            DCC.SqlDC.SDC.GetTable("insert into T_Purchasing(T_DjNumber,T_ItemNumber,T_ShopNumber,T_Objective,T_Number,T_Name,T_Quantity,T_Model,T_DepotName,T_SupplierName,T_Re,T_Principal,T_date,T_uid,T_Choose) values('" + txt_title.Text + "','" + com_bh.Text + "','" + com_cp.Text + "','" + txt_md.Text + "','" + com_bh.Text + "','" + com_cp.Text + "','" + dataGridView2.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView2.Rows[i].Cells[1].Value.ToString() + "','" + com_kwm.Text + "','" + com_gys.Text + "','" + txt_bz.Text + "','" + com_zdr.Text + "','" + label2.Text + "'," + com_zdr.SelectedValue + "," + 4 + ")");
                        }
                        DialogResult result = MessageBox.Show("添加成功,是否继续添加?", "采购管理", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                        if (result == DialogResult.OK)
                        {
                            this.Show();
                        }
                        else if (result == DialogResult.Cancel)
                        {
                            this.Close();
                            prices pl = new prices();
                            pl.ShowDialog();
                        }
                    }
                   
                }
                else
                {
                    MessageBox.Show("请填写完整");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("请正确填写");
            }        }//我写的添加但是DATAGIRDVIEW内的CHECKBOX不能选中,选择后勾就没了当然也不能多选,求高手帮忙~~~

解决方案 »

  1.   

    DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
      newColumn.HeaderText = "选择";
      dataGridView.Columns.Insert(0, newColumn);
      newColumn.InheritedStyle.Alignment = DataGridViewContentAlignment.TopCenter;
      dataGridView.MultiSelect = true;
    foreach (DataGridViewRow dr in this.dataGridView1.Rows)  
      {  
      DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];  
      if ((bool)cbx.FormattedValue)  
      {  }
      }
      

  2.   

    是不是勾选的时候又postback了?
    将不用重新装载的代码写到
    if(!postback)
    {....}
      

  3.   

    好像select 到datagridview的时候,在select语句里面加个bool类型值为false的字段,到datagridview中的时候,就有了checkbox列吧。
      

  4.   

    现在CHECKBOX是有的,但是现在只能单选而且选好在点击到其他控件,他又不选中了。更不能多选