Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向DataGridView添加复选框并选中/取消选中它们_C#_.net_Winforms_Datagridview_Checkbox - Fatal编程技术网

C# 向DataGridView添加复选框并选中/取消选中它们

C# 向DataGridView添加复选框并选中/取消选中它们,c#,.net,winforms,datagridview,checkbox,C#,.net,Winforms,Datagridview,Checkbox,我想为DataGridView中的每一行添加一个复选框。 DataGridView将动态填充,填充时,应具有一个名为“Select”的新列,该列应包含复选框 我使用了这个代码,但它不起作用 DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn(); clsCol1.HeaderText = "Select"; clsCol1.ValueType = typeof(System.String); this.dataGridView1

我想为
DataGridView
中的每一行添加一个复选框。
DataGridView
将动态填充,填充时,应具有一个名为“Select”的新列,该列应包含复选框

我使用了这个代码,但它不起作用

DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
clsCol1.HeaderText = "Select";
clsCol1.ValueType = typeof(System.String);
this.dataGridView1.Columns.Add(clsCol1);
我也试过这个代码

dataGridView1.Columns.Clear(); DataGridViewCheckBoxColumn c = new DataGridViewCheckBoxColumn();
{
    column.HeaderText = "Selected";
    column.Name = "Selected";
    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    column.FlatStyle = FlatStyle.Standard;
    column.ThreeState = false;
    column.CellTemplate = new DataGridViewCheckBoxCell();
    column.CellTemplate.Style.BackColor = Color.Beige;
}
dataGridView1.Columns.Insert(0, column)
在填充了
DataGridView
之后,我想创建两个按钮来选中所有复选框和取消选中所有复选框


我对C#非常缺乏经验,因此任何答案都将不胜感激。

在复选框的
CheckChanged事件上,您需要选择/取消选择全部

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    dataGridView1.Rows[i].Cells["CheckBoxColumnName"].Value = chkSelectAll.Checked;
}
for(int i=0;i

在此之后,您可能想在DataGridView的一行未选中时取消选中SelectAll复选框

请注意,
DataGridView
DataGrid
是不同的控件。前者旨在取代后者,但两者仍然可用。弄清楚你用的是哪一个很重要。你得到答案了吗?如果你找到了,把它标记为答案,如果你自己找到了答案,你可以编辑你的帖子来解释你做了什么。如果您仍在寻找解决问题的方法,可以提供更多详细信息。(您可以针对所有问题执行此操作)