在c#的winform中的datagrid中有一列为DataGridBoolColumn(有"√")类型,当双击此列时,它的checked可改变(没有"√"),当当双击另一行的此列时,它又恢复原样(有"√"),应如何实现双击此列后没有"√"的变为有"√",有"√"的变为有"√"?
DataGridBoolColumn boolCol= new DataGridBoolColumn(); 
boolCol.MappingName = "cxz2"; 
boolCol.HeaderText = "选择2"; 
boolCol.Width = 50; 
boolCol.AllowNull=false;
boolCol.NullValue=false;
boolCol.FalseValue=false;
boolCol.TrueValue=true;
//添加事件处理器 
// boolCol.FalseChanged +=new EventHandler(boolCol_FalseChanged);
// boolCol.TrueValueChanged +=new EventHandler(boolCol_TrueValueChanged); ts1.GridColumnStyles.Add(boolCol);  dataGrid1.TableStyles.Add(ts1);

解决方案 »

  1.   


    源码下载:在DataGridColumnStyle的单元格中使用DateTimePicker、ComboBox、NumericUpDown等控件
    http://community.csdn.net/Expert/topic/3522/3522038.xml?temp=.2270777
      

  2.   

    /// <summary>
    /// 对DataGrid中的CheckBox进行值判断,并对数据库做出修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dataSource_CurrentCellChanged(object sender, System.EventArgs e)
    {
    try
    {
    string temp = dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),0].ToString();
    this.dataGridView.Select(dataGridView.CurrentCell.RowNumber);
    DataAccess data = new DataAccess();
    //判断是否点中的是CheckBox列,以保证其值正确更改
    if(dataGridView.CurrentCell.ColumnNumber == 12)
    {
    //判断CheckBox单元格的值,并根据其值更新数据库
    if(dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),12].ToString() == "False")
    {
    dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),12] = true;
    string query_sql = "update accounttype set accflag = '1' where id = '" + temp + "'";
    data.ExecuteSQL(query_sql);
    }
    else if(dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),12].ToString() == "True")
    {
    dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),12] = false;
    string query_sql = "update accounttype set accflag = '0' where id = '" + temp + "'";
    data.ExecuteSQL(query_sql);
    }
    }
    else
    {
    //选中的不是CheckBox列就跳出此过程
    return;
    }
    }
    catch
    {
    MessageBox.Show("表格中未有任何记录!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    } }