帮助添加复选框列的DataGridView窗口形式 [英] Help with adding checkbox column to DataGridView in window form
问题描述
我想一个复选框列添加到一个DataGridView在一个简单的窗口窗体应用程序。
I am trying to add a checkbox column to a DataGridView in a simple window forms application.
我从数据库中拉回来一些数据使用ADO.NET,将放入一个数据表中,然后设置datagridview的数据源的数据表。那么我想添加一个复选框列第二列。到目前为止,我有这个code,似乎工作:
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:
' 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)
这code'工作',我得到MyDataGridView显示在表中的正确位置的复选框列中的数据。
This code 'works' and I get MyDataGridView to show the data with the checkbox column in the correct position in the table.
然而,出于某种原因,我无法检查任何在DataGridView的复选框?我已经试过很多东西(egaltering列的只读状态),但不能让它的工作。
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.
有什么明显的,我很想念?
Is there something obvious that I am missing?
推荐答案
在DataGridView的属性添加新列方式:
Add new column in the properties of the DataGridView by:
- 选择从属性栏 面板,双击它
- 然后再选择的的添加... 的按钮
- 然后设置新列的的不作承诺 列的
- 给它一个名称,并选择它的类型 DataGridViewCheckBoxColumn 的
- 设置你想要的头部和制作 确定说的的只读的是的不可以 选中。
- Choosing Columns from properties panel and double click on it
- then choose " Add... " button
- then set the new column as " Unbound Column "
- Give it a name and choose its type " DataGridViewCheckBoxColumn "
- Set the header you want and make sure that " read only " is not selected.
这就是它。
(如果数据库字段(SQL Server)的类型是位的则在DataGridView会自动将其映射到DataGridView作为一个复选框,而不是一个文本框,没有编码的必要。)
(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.)
这篇关于帮助添加复选框列的DataGridView窗口形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!