如何以编程方式在C#中的datagridview中添加复选框单元格 [英] How to add check box cell in datagridview in C# programmatically
问题描述
Hello All,
我已经在C#的Windows窗体中添加了datagridview(name dgvTestLoader)。我添加了以下列集合
1)第一列:列类型:DataGridViewCheckBoxColumn
2)第二列:列类型:DataGridViewTextBoxColumn
下面是在点击按钮时向此datagridview添加行的代码
Hello All ,
I have added datagridview(name dgvTestLoader) in windows form of C#. I have added columns collection as below
1) First column : column type : DataGridViewCheckBoxColumn
2) Second column : column type : DataGridViewTextBoxColumn
Below is the code which is adding row to this datagridview on click on button
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvTestLoader);
row.Cells[0].Value = CheckState.Checked;
row.Cells[1].Value = Txtname.text;
dgvTestLoader.Rows.Add(row);
执行此代码没有错误,但执行后第一行没有显示datagridview中的复选框。
让我知道此代码中的更正,将第一列的列类型更改为复选框。
谢谢提前完成。
There is no error to execute this code but after execution first row didn't display check box in datagridview.
Let me know correction in this code to change column type of first column as check box.
Thank you in advance.
推荐答案
在Inseert ROws之后首先创建列
First Create Column After Inseert ROws
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "X";
checkColumn.HeaderText = "X";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
dataGridView1.Columns.Add(checkColumn);
检查此链接
添加CheckBox列到您的DataGrid [ ^ ]
这篇关于如何以编程方式在C#中的datagridview中添加复选框单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!