C# DataGridView 复选框列 - 值和功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1237829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView checkbox column - value and functionality
提问by David Archer
I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.
我在 C# 表单中向 DataGridView 添加了一个复选框列。该功能需要是动态的 - 您选择一个客户并调出他们所有可以服务的项目,然后您选择这次要服务的项目。
Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:
无论如何,代码现在将在 DGV 的开头添加一个 chckbox。我需要知道的是以下内容:
1) How do I make it so that the whole column is "checked" by default? 2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?
1)如何使整个列在默认情况下被“选中”?2) 当我单击 DGV 正下方的按钮时,如何确保我只从“已检查”行获取值?
Here's the code to get the column inserted:
这是插入列的代码:
DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
doWork.HeaderText = "Include Dog";
doWork.FalseValue = "0";
doWork.TrueValue = "1";
dataGridView1.Columns.Insert(0, doWork);
So what next? Any help would be greatly appreciated!
那么接下来呢?任何帮助将不胜感激!
采纳答案by SwDevMan81
There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:
foreach (DataGridViewRow row in dataGridView1.Rows) { row.Cells[CheckBoxColumn1.Name].Value = true; }
The Click event might look something like this:
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 }
没有办法直接做到这一点。将数据放入网格后,您可以遍历行并检查每个框,如下所示:
foreach (DataGridViewRow row in dataGridView1.Rows) { row.Cells[CheckBoxColumn1.Name].Value = true; }
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 }
回答by Nazeer
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());
}
best solution to find if the checkbox in the datagridview is checked or not.
查找 datagridview 中的复选框是否被选中的最佳解决方案。
回答by andzep
it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:
我花了很长时间才弄清楚如何做到这一点,而不必遍历所有记录。我有一个绑定的 datagridview-source,除了复选框列之外的所有字段都被绑定。所以我没有/需要一个循环来添加每一行,我不想只为这个目的创建一个。所以经过多次尝试,我终于得到了它。它实际上也很简单:
First you add a new .cs File to your project with a custom-checkbox cell, e.g.
首先,您使用自定义复选框单元将新的 .cs 文件添加到您的项目中,例如
DataGridViewCheckboxCellFilter.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;
}
}
}
After this, on your GridView, where you add the checkbox-column, you do:
在此之后,在您添加复选框列的 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);
And that's it! Everytime your checkboxes get added in a new row, they'll be set to true. Enjoy!
就是这样!每次您的复选框被添加到新行时,它们都会被设置为 true。享受!
回答by Rob
Here's a one liner answer for this question
这是这个问题的单行答案
List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();
回答by Amin Mansuri
If you have a gridview containing more than one checkbox .... you should try this ....
如果你有一个包含多个复选框的 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());
}
}
}
回答by Atul Jain
If you try it on CellContentClick
Event
如果您在CellContentClick
Event上尝试
Use:
用:
dataGridView1.EndEdit(); //Stop editing of cell.
MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
回答by Roozi
To test if the column is checked or not:
要测试该列是否被选中:
for (int i = 0; i < dgvName.Rows.Count; i++)
{
if ((bool)dgvName.Rows[i].Cells[8].Value)
{
// Column is checked
}
}
回答by DarkChowder
1) How do I make it so that the whole column is "checked" by default?
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) How can I make sure I'm only getting values from the "checked" rows?
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
}
}
回答by Ahmed Soliman Flasha
if u make this column in sql database (bit) as a data type u should edit this code
如果您在 sql 数据库(位)中将此列作为数据类型,您应该编辑此代码
DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
doWork.HeaderText = "Include Dog";
doWork.FalseValue = "0";
doWork.TrueValue = "1";
dataGridView1.Columns.Insert(0, doWork);
with this
有了这个
DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
doWork.HeaderText = "Include Dog";
doWork.FalseValue = "False";
doWork.TrueValue = "True";
dataGridView1.Columns.Insert(0, doWork);
回答by Nasrudeen
It is quite simple
这很简单
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;
}