复选框以选择或取消选择列SQL的所有项目

时间:2019-04-01 11:04:37

标签: c# sql wpf

我有一个类型为bit(布尔值)的SQL列,我可以从数据网格中选中或取消选中某些选项,其中显示了这些复选框。现在,我希望有一个复选框可以选择或取消选择数据网格中的所有复选框。我已经找到了这段代码:

private void Chk_All_Checked(object sender, RoutedEventArgs e)
{
  sqliteCon.Open();
  if (sqliteCon.State == System.Data.ConnectionState.Open)
  {

    CORRENTE

    string q = @"UPDATE tabL
                     SET selection = (CASE 
                                        WHEN (SELECT selection FROM tabL ) = 0
                                        THEN 1
                                        ELSE 0
                                      END)
                     WHERE ?????????????????????";

    SqlCommand cmd = new SqlCommand(q, sqliteCon);
    cmd.ExecuteNonQuery();
    MessageBox.Show("All Items Checked");


  }
  sqliteCon.Close();
}

“ WHERE idL = @ CURRENT”是错误的

这是另一种尝试,但效果不佳:

private void Chk_All_Checked(object sender, RoutedEventArgs e)
{
  sqliteCon.Open();
  if (sqliteCon.State == System.Data.ConnectionState.Open)
  {
    string q = @"UPDATE tabL
                     SET selection = 1";
    SqlCommand cmd = new SqlCommand(q, sqliteCon);
    cmd.ExecuteNonQuery();
    MessageBox.Show("All Items Checked");


    string q2 = @"UPDATE tabL
                     SET selection = 0";
    SqlCommand cmd2 = new SqlCommand(q2, sqliteCon);
    cmd2.ExecuteNonQuery();
    MessageBox.Show("All Items DeChecked");


  }
  sqliteCon.Close();
}

2 个答案:

答案 0 :(得分:1)

378查看0个解决方案,但我自己找到了一个 chkbox对象具有两个事件处理程序,一个用于检查,另一个用于欺骗。

private void Chk_All_Checked(object sender, RoutedEventArgs e)
{
  sqliteCon.Open();
  if (sqliteCon.State == System.Data.ConnectionState.Open)
  {
    if (chk_All.IsChecked == true) { 
    string q = @"UPDATE tabList
                     SET selection = 1";
    SqlCommand cmd = new SqlCommand(q, sqliteCon);
    cmd.ExecuteNonQuery();
    MessageBox.Show("All Items Checked");
    }


  }
  sqliteCon.Close();
}



  private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
  sqliteCon.Open();
  if (sqliteCon.State == System.Data.ConnectionState.Open)
  {
    if (chk_All.IsChecked == false)
    {
      string q2 = @"UPDATE tabList
                     SET selection = 0";
      SqlCommand cmd2 = new SqlCommand(q2, sqliteCon);
      cmd2.ExecuteNonQuery();
      MessageBox.Show("All Items DeChecked");
    }
  }
  sqliteCon.Close();
}

答案 1 :(得分:0)

使用以下代码:

private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {         
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Index == e.RowIndex)
            {
                row.Cells["checkBoxColumn"].Value = 
                !Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue);
            }
            else
            {
                row.Cells["checkBoxColumn"].Value = false;
            }
        }
    }
}