帮助添加复选框列的DataGridView窗口形式 [英] Help with adding checkbox column to DataGridView in window form

查看:318
本文介绍了帮助添加复选框列的DataGridView窗口形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个复选框列添加到一个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:

  1. 选择从属性栏 面板,双击它
  2. 然后再选择的添加... 按钮
  3. 然后设置新列的不作承诺 列
  4. 给它一个名称,并选择它的类型 DataGridViewCheckBoxColumn
  5. 设置你想要的头部和制作 确定说的只读是的不可以 选中。
  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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆