简体   繁体   English

将单元格更改为DataGridView中的按钮

[英]Change a Cell into a Button in DataGridView

I have a databound DataGridView. 我有一个数据绑定的DataGridView。 I want the cells of the two columns to contain a button instead of a regular cell. 我希望两列的单元格包含一个按钮而不是常规单元格。
Edit: Solved, now I need to figure out how to disable the button when a Cell contains NULL. 编辑:解决了,现在我需要弄清楚当单元格包含NULL时如何禁用按钮。

To enable adding of Columns to a specified position to a DataBound Datagridview, we have to set the Datagridviews AutoGenerateColumns Property to False before adding the Column. 为了能够将列添加到DataBound Datagridview的指定位置,我们必须在添加列之前将Datagridviews AutoGenerateColumns属性设置为False。
Without doing so, all Columns that will be added will be placed on the right most end of the Datagridview and it will affect the first Column too. 如果不这样做,将要添加的所有列都将放置在Datagridview的最右端,这也会影响第一列。

But remember that the Colummn.Index Property will be affected. 但是请记住Colummn.Index属性将受到影响。

dgv_dt is a DataTable  
dgv is the DataGridView  

dgv.AutoGenerateColumns=True

dgv.DataSource=dgv_dt
dgv.ClearSelection
dgv.Columns(0).HeaderText="FirstName"
dgv.Columns(1).HeaderText="Company ID"
dgv.Columns(2).HeaderText="Recent Picture"
dgv.Columns(2).Visible=False
dgv.Columns(3).HeaderText="Address"
dgv.Columns(4).HeaderText="Alive"

dgv.AutoGenerateColumns=False

Dim btn As New DataGridViewButtonColumn()
btn.HeaderText = "Click Data"
btn.Text = "Click Here"
btn.Name = "btn"
dgv.Columns.Insert(2,btn)

The code above will display the Datagridview and its Column.Index like this: 上面的代码将显示Datagridview及其Column.Index,如下所示:

dgv.Columns(1) First Name
dgv.Columns(2) Company ID
dgv.Columns(3) Recent Picture 'This column is hidden, if this Contains NULL, ButtonCell is enabled/clickable
dgv.Columns(0) ButtonCell
dgv.Columns(4) Address
dgv.Columns(5) Alive

The Columns Company ID and Recent Picture contains a BLOB or NULL, if its a BLOB, the cell will be an Enabled Button and a Disabled Button if its NULL. 公司ID最近的图片包含一个BLOB或NULL,如果它是BLOB,则该单元将是一个Enabled按钮,如果它是NULL,则为Disabled按钮。

Set existing column to hidden .Visible = false . 将现有列设置为hidden .Visible = false
Then add DataGridViewButtonColumn and check value of CompanyID column 然后添加DataGridViewButtonColumn并检查CompanyID列的值

if BLOB then button column enabled 如果BLOB则启用按钮列
if NULL then button column disabled 如果为NULL则禁用按钮列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM