从问卷样式为DataGridView的多个复选框列中仅选择一个复选框 [英] Select only one checkbox from multiple checkbox columns in questionnaire style DataGridView

查看:57
本文介绍了从问卷样式为DataGridView的多个复选框列中仅选择一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个显示 DataGridView 的应用程序,其中包含一系列问题。
dgv结构由一个用于问题文本的字符串列和三个用于答案的布尔/复选框列组成(是,否,不适用)。
每个问题都显示在其自己的行上。



我希望我的程序仅允许用户选择是,否或仅N /。每行A。



我想我需要在选中一个选项时取消选中其他复选框,但是我不太确定该怎么做。 / p>

我已经设置了 CellValueChanged CellContentClick 事件,但是我我不确定要实现所需功能所需的代码。



DataGridView绑定到DataTable。



我到目前为止的代码:

  private void dgvQuestions_CellValueChanged(object sender,DataGridViewCellEventArgs e)
{
int columnIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;

DataGridViewCheckBoxCell chkYes = dgvQuestions.Rows [rowIndex] .Cells [2] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell chkNo = dgvQuestions.Rows [rowIndex] .Cells [3] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell chkNA = dgvQuestions.Rows [rowIndex] .Cells [4] as DataGridViewCheckBoxCell;

if(Convert.ToBoolean(chkYes.Value)== true)
{

}

if(Convert.ToBoolean( chkNo.Value)== true)
{

}

if(Convert.ToBoolean(chkNA.Value)== true)
{

}
}

私人无效dgvQuestions_CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
dgvQuestions.CommitEdit(DataGridViewDataErrorContexts.Commit);
}


解决方案

看来您有 CellContentClick 设置正确,但是,如果网格中还有其他列,那么检查以确保其内容被单击的单元格实际上是一个复选框单元格。否则,代码可能会不必要地设置单元格值。

  private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e){
字符串colName = dataGridView1.Columns [e.ColumnIndex] .Name;
if(colName ==是 || colName ==否 || colName == N / A){
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

CellValueChanged 事件,代码再次应仅检查复选框值。另外,我假设必须检查至少一(1)个单元格。例如,如果最初选中 N / A单元格,则用户取消选中该单元格,则该行将没有选中任何复选框。这是代码中的最后检查,如果用户取消选中 N / A单元格,而所有复选框都保持未选中,则代码将选中 N / A单元格。同样,重要的是在我们更改 CellValueChanged CellValueChanged 事件。 >事件避免重入。像…

 私有无效dataGridView1_CellValueChanged(对象发送者,DataGridViewCellEventArgs e){
if(e.RowIndex> = 0&& e.ColumnIndex> = 0){
字符串colName = dataGridView1.Columns [e.ColumnIndex] .Name;
bool checkValue;
dataGridView1.CellValueChanged-=新的DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
switch(colName){
case Yes:
checkValue =(bool)dataGridView1.Rows [e.RowIndex] .Cells [ Yes]。Value;
if(checkValue == true){
dataGridView1.Rows [e.RowIndex] .Cells [ No]。Value = false;
dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value = false;
}
else {
dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value = true;
}
休息;
case No:
checkValue =(bool)dataGridView1.Rows [e.RowIndex] .Cells [ No]。Value;
if(checkValue == true){
dataGridView1.Rows [e.RowIndex] .Cells [ Yes]。Value = false;
dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value = false;
}
else {
dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value = true;
}
休息;
case N / A:
checkValue =(bool)dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value;
if(checkValue == true){
dataGridView1.Rows [e.RowIndex] .Cells [ Yes]。Value = false;
dataGridView1.Rows [e.RowIndex] .Cells [ No]。Value = false;
}
else {
if((bool)dataGridView1.Rows [e.RowIndex] .Cells [ Yes]。Value == false&&
(bool )dataGridView1.Rows [e.RowIndex] .Cells [ No]。Value == false){
dataGridView1.Rows [e.RowIndex] .Cells [ N / A]。Value = true;
}
}
休息;
默认值:
休息时间;
}
dataGridView1.CellValueChanged + =新的DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
}

下面是一个简单的示例,其中包含三列是,否

  public Form1(){
InitializeComponent();和不适用复选框列。
}

私有无效Form1_Load(对象发送者,EventArgs e){
dataGridView1.DataSource = GetTable();
}

私有DataTable GetTable(){
DataTable dt = new DataTable();
dt.Columns.Add( Yes,typeof(bool));
dt.Columns.Add( No,typeof(bool));
dt.Columns.Add( N / A,typeof(bool));
for(int i = 0; i< 10; i ++){
dt.Rows.Add(false,false,true);
}
return dt;
}

希望这会有所帮助。


I've create an application which displays a DataGridView with a series of questions. The dgv structure consists of one string column for the question text and three bool/checkbox columns for the answers (yes, no, N/A). Each single question is displayed on its own row.

I would like my program to only allow the user to select only Yes, only No or only N/A on each row.

I think I would need to uncheck the other checkbox options when one option is checked but I'm not too sure on how to do this.

I've setup CellValueChanged and CellContentClick events but I'm unsure of the code needed to achieve the desired functionality.

DataGridView is bound to a DataTable.

Code I have so far:

private void dgvQuestions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    int columnIndex = e.ColumnIndex;
    int rowIndex = e.RowIndex;

    DataGridViewCheckBoxCell chkYes = dgvQuestions.Rows[rowIndex].Cells[2] as DataGridViewCheckBoxCell;
    DataGridViewCheckBoxCell chkNo = dgvQuestions.Rows[rowIndex].Cells[3] as DataGridViewCheckBoxCell;
    DataGridViewCheckBoxCell chkNA = dgvQuestions.Rows[rowIndex].Cells[4] as DataGridViewCheckBoxCell;    

    if (Convert.ToBoolean(chkYes.Value) == true)
    {

    }

    if (Convert.ToBoolean(chkNo.Value) == true)
    {

    }

    if (Convert.ToBoolean(chkNA.Value) == true)
    {

    }
}

private void dgvQuestions_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dgvQuestions.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

解决方案

It appears you have the CellContentClick set up properly, however, if there are other columns in the grid, then, it may be beneficial if you check to make sure that the cell whose content was clicked is actually one of the check box cells. Otherwise the code may be setting the cells value unnecessarily.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
  string colName = dataGridView1.Columns[e.ColumnIndex].Name;
  if (colName == "Yes" || colName == "No" || colName == "N/A") { 
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
  }
}

In the CellValueChanged event, again the code should check for only the check box values. In addition, I would assume that at least ONE (1) of the cells MUST be checked. Example, if the "N/A" cell is originally checked, then the user "unchecks" that cell, then the row would have NO check boxes checked. This is the final check in the code such that if the user "unchecks" the "N/A" cell AND this leaves ALL check boxes "unchecked", then, the code will "check" the "N/A" cell. Also, it is important to "turn OFF" the CellValueChanged event before we change any of the check box values IN the CellValueChanged event to avoid reentrant. Something like…

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  if (e.RowIndex >= 0 && e.ColumnIndex >= 0) {
    string colName = dataGridView1.Columns[e.ColumnIndex].Name;
    bool checkValue;
    dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
    switch (colName) {
      case "Yes":
        checkValue = (bool)dataGridView1.Rows[e.RowIndex].Cells["Yes"].Value;
        if (checkValue == true) {
          dataGridView1.Rows[e.RowIndex].Cells["No"].Value = false;
          dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value = false;
        }
        else {
          dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value = true;
        }
        break;
      case "No":
        checkValue = (bool)dataGridView1.Rows[e.RowIndex].Cells["No"].Value;
        if (checkValue == true) {
          dataGridView1.Rows[e.RowIndex].Cells["Yes"].Value = false;
          dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value = false;
        }
        else {
          dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value = true;
        }
        break;
      case "N/A":
        checkValue = (bool)dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value;
        if (checkValue == true) {
          dataGridView1.Rows[e.RowIndex].Cells["Yes"].Value = false;
          dataGridView1.Rows[e.RowIndex].Cells["No"].Value = false;
        }
        else {
          if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Yes"].Value == false &&
              (bool)dataGridView1.Rows[e.RowIndex].Cells["No"].Value == false) {
            dataGridView1.Rows[e.RowIndex].Cells["N/A"].Value = true;
          }
        }
        break;
      default:
        break;
    }
    dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);
  }

Below is a simple example with the three columns "Yes", "No" and "N/A" check box columns.

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  dataGridView1.DataSource = GetTable();
}

private DataTable GetTable() {
  DataTable dt = new DataTable();
  dt.Columns.Add("Yes", typeof(bool));
  dt.Columns.Add("No", typeof(bool));
  dt.Columns.Add("N/A", typeof(bool));
  for (int i = 0; i < 10; i++) {
    dt.Rows.Add(false, false, true);
  }
  return dt;
}

Hope this helps.

这篇关于从问卷样式为DataGridView的多个复选框列中仅选择一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆