vb.net 帮助以窗口形式向 DataGridView 添加复选框列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1035102/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:16:46  来源:igfitidea点击:

Help with adding checkbox column to DataGridView in window form

vb.netwinformsado.net

提问by Peter Mankge

I am trying to add a checkbox column to a DataGridView in a simple window forms application.

我正在尝试在一个简单的窗口窗体应用程序中向 DataGridView 添加一个复选框列。

I am pulling back some data from a database using ADO.NET, putting into a datatable, and then setting the datagridview datasource to the datatable. I then want to add a checkbox column as the second column. So far I have this code that seems to work:

我正在使用 ADO.NET 从数据库中提取一些数据,放入数据表中,然后将 datagridview 数据源设置为数据表。然后我想添加一个复选框列作为第二列。到目前为止,我有这个似乎有效的代码:

' Code here to connect to database
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)

MainForm.MyDataGridView.DataSource = dt

Dim ChkBox As New DataGridViewCheckBoxColumn

ChkBox.FlatStyle = FlatStyle.Standard
MainForm.MyDataGridView.Columns.Insert(1, ChkBox)

This code 'works' and I get MyDataGridView to show the data with the checkbox column in the correct position in the table.

这段代码“有效”,我让 MyDataGridView 在表格中的正确位置显示带有复选框列的数据。

However, for some reason, I cannot check any of the check boxes in the DataGridView? I have tried lots of things (e.g.altering the readonly state of the column) but cannot get it to work.

但是,由于某种原因,我无法选中 DataGridView 中的任何复选框?我已经尝试了很多东西(提高了列的只读状态)但无法让它工作。

Is there something obvious that I am missing?

有什么明显的我遗漏了吗?

回答by Wahid Bitar

Add new column in the properties of the DataGridView by:

通过以下方式在 DataGridView 的属性中添加新列:

  1. Choosing Columns from properties panel and double click on it
  2. then choose " Add..." button
  3. then set the new column as " Unbound Column"
  4. Give it a name and choose its type " DataGridViewCheckBoxColumn"
  5. Set the header you want and make surethat " read only" is notselected.
  1. 从属性面板中选择列并双击它
  2. 然后选择“添加...”按钮
  3. 然后将新列设置为“未绑定列
  4. 给它一个名字并选择它的类型“ DataGridViewCheckBoxColumn
  5. 设置所需的标题并确保选择“只读” 。

that's it.

就是这样。

(If the database field (in SQL Server) is of type 'bit' then the the datagridview automatically maps it to the datagridview as a checkbox instead of a textbox. No coding necessary.)

(如果数据库字段(在 SQL Server 中)是“位”类型,那么 datagridview 会自动将它映射到 datagridview 作为复选框而不是文本框。无需编码。)

回答by Peter Mankge

Private Sub ADD_Column()

  Dim AddColumn As New DataGridViewCheckBoxColumn

  With AddColumn
    .HeaderText = "ColumnName"
    .Name = "Column Name that will be displayed"
    .Width = 80
  End With

  dgAdmin.Columns.Insert(1, AddColumn)

End Sub

回答by Peter Mankge

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
   if(dataGridView1.Columns.Count == 13 )
   {
       DataGridViewCheckBoxColumn chkSelect = new DataGridViewCheckBoxColumn();
       {
           chkSelect.HeaderText = "Select All";
           chkSelect.Name = "chkSelect";
           chkSelect.Selected = false;


       }
       dataGridView1.Columns.Insert(13, chkSelect);
   }

}    

回答by Kashan Malick

Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)

回答by Hilal Al-Rajhi

I have had this issue once, but I solved it. L load data from dataset and the fill the datagridview. I was setting the readOnly property of the datagridview = True, which means you cant modify the data in the datagridview. Just set the AllowUserToAddColumnto False and make readOnly = Falseand this will work.

我曾经遇到过这个问题,但我解决了。l 从dataset加载数据并填充datagridview。我正在设置 的 readOnly 属性datagridview = True,这意味着您不能修改datagridview. 只需将 设置AllowUserToAddColumn为 False 并制作readOnly = False,这将起作用。

回答by Harpal

Try this:

尝试这个:

        DataGridViewCheckBoxColumn chkBoxCol = new DataGridViewCheckBoxColumn();
        DataGridView1.Columns.Add(chkBoxCol);