DataGridView复选框列 - 值和功能

时间:2009-08-06 09:17:59

标签: c# winforms datagridview checkbox

我在C#表单中为DataGridView添加了一个复选框列。该功能需要是动态的 - 您选择一个客户并显示他们可以维修的所有物品,并选择您希望这次服务中的哪一个。

无论如何,代码现在将添加一个chckbox到DGV的开头。我需要知道的是:

1)如何制作它以便默认“检查”整列? 2)当我点击DGV正下方的按钮时,如何确保我只从“已检查”的行中获取值?

以下是插入列的代码:

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "0";
            doWork.TrueValue = "1";
            dataGridView1.Columns.Insert(0, doWork);

接下来是什么? 任何帮助将不胜感激!

11 个答案:

答案 0 :(得分:37)

  1. 没有办法直接这样做。将数据放入网格后,您可以遍历行并检查每个框,如下所示:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[CheckBoxColumn1.Name].Value = true;
    }
    
  2. Click事件可能如下所示:

    private void button1_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
            {
                rows_with_checked_column.Add(row);
            }
        }
        // Do what you want with the check rows
    }
    

答案 1 :(得分:14)

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

    if (ch1.Value == null)
        ch1.Value=false;
    switch (ch1.Value.ToString())
    {
        case "True":
            ch1.Value = false;
            break;
        case "False":
            ch1.Value = true;
            break;
    }
    MessageBox.Show(ch1.Value.ToString());
}

查找是否选中了datagridview中的复选框的最佳解决方案。

答案 2 :(得分:4)

以下是此问题的单行答案

List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();

答案 3 :(得分:3)

我花了很长时间才弄清楚如何做到这一点,而不必遍历所有记录。我有一个绑定的datagridview-source,除了checkbox-column之外,所有字段都被绑定。所以我没有/需要一个循环来添加每一行,我不想只为这个purpuse创建一个。经过大量的努力,我终于明白了。它实际上也非常简单:

首先,使用自定义复选框单元格将新的.cs文件添加到项目中,例如

DataGridViewCheckboxCellFilter.cs:

using System.Windows.Forms;

namespace MyNamespace {
    public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
        public DataGridViewCheckboxCellFilter() : base() {
            this.FalseValue = 0;
            this.TrueValue = 1;
            this.Value = TrueValue;
        }
    }
}

在此之后,在GridView上添加复选框列,您可以执行以下操作:

// add checkboxes
DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
{
    col_chkbox.HeaderText = "X";
    col_chkbox.Name = "checked";
    col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();                
}
this.Columns.Add(col_chkbox);

就是这样!每次将复选框添加到新行时,它们都将设置为true。 享受!

答案 4 :(得分:3)

如果您在CellContentClick活动

上试用

使用:

dataGridView1.EndEdit();  //Stop editing of cell.
MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

答案 5 :(得分:1)

如果你有一个包含多个复选框的gridview .... 你应该试试这个......

Object[] o=new Object[6];

for (int i = 0; i < dgverlist.RowCount; i++)
{
    for (int j = 2; j < dgverlist.ColumnCount; j++)
    {
        DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
        ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

        if (ch1.Value != null)
        {
           o[i] = ch1.OwningColumn.HeaderText.ToString();

            MessageBox.Show(o[i].ToString());
        }
    }
}

答案 6 :(得分:0)

测试是否检查了列:

for (int i = 0; i < dgvName.Rows.Count; i++)
{
    if ((bool)dgvName.Rows[i].Cells[8].Value)
    {
    // Column is checked
    }
}

答案 7 :(得分:0)

1)如何制作它以便默认“检查”整列?

var doWork = new DataGridViewCheckBoxColumn();
doWork.Name = "IncludeDog" //Added so you can find the column in a row
doWork.HeaderText = "Include Dog";
doWork.FalseValue = "0";
doWork.TrueValue = "1";

//Make the default checked
doWork.CellTemplate.Value = true;
doWork.CellTemplate.Style.NullValue = true;

dataGridView1.Columns.Insert(0, doWork);

2)我怎样才能确保我只从“已检查”的行中获取值?

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.IsNewRow) continue;//If editing is enabled, skip the new row

    //The Cell's Value gets it wrong with the true default, it will return         
    //false until the cell changes so use FormattedValue instead.
    if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
    {
        //Do stuff with row
    }
}

答案 8 :(得分:0)

如果你把sql数据库(bit)中的这个列作为数据类型你应该编辑这段代码

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "0";
            doWork.TrueValue = "1";
            dataGridView1.Columns.Insert(0, doWork);

用这个

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "False";
            doWork.TrueValue = "True";
            dataGridView1.Columns.Insert(0, doWork);

答案 9 :(得分:0)

这很简单

DataGridViewCheckBoxCell checkedCell = (DataGridViewCheckBoxCell) grdData.Rows[e.RowIndex].Cells["grdChkEnable"];

                bool isEnabled = false;
                if (checkedCell.AccessibilityObject.State.HasFlag(AccessibleStates.Checked))
                {
                    isEnabled = true;
                }
if(isEnabled)
 {
   // do your business process;
 }

答案 10 :(得分:0)

如果由于绑定了BIT类型的记录集而已经创建了列,则该列的类型还是文本。我发现的解决方案是删除该列,然后将其替换为具有相同绑定数据的“ DataGridViewCheckBoxColumn”。

            DataGridViewColumn oldCol = dgViewCategory.Columns["mycolumn"];
            int chkIdx = oldCol.Index;
            DataGridViewCheckBoxColumn chkCol = new DataGridViewCheckBoxColumn();
            chkCol.HeaderText = oldCol.HeaderText;
            chkCol.FalseValue = "False";
            chkCol.TrueValue = "True";
            chkCol.DataPropertyName = oldCol.DataPropertyName;
            chkCol.Name = oldCol.Name;
            dgViewCategory.Columns.RemoveAt(chkIdx);
            dgViewCategory.Columns.Insert(chkIdx, chkCol);