如何在VB.net中添加一个UserControl到DataGridView,并且控件总是显示? [英] How to add a UserControl to a DataGridView in VB.net, and have the control always showing?

查看:1507
本文介绍了如何在VB.net中添加一个UserControl到DataGridView,并且控件总是显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个用户控件,它基本上是一个带有一堆标签,按钮和事件处理程序的面板。我想使用这些控件来填充DataGridView。



我已按照



代码:



这里是类似vb的伪代码。

 '这不是VB,这是类似vb的伪代码
Private Sub Form_Load(sender as object,e作为EventArgs)
'获取数据
'显示网格中的数据

'对于每一行
对于每一行作为DataGridViewRow在this.categoryDataGridView.Rows
'创建一个控件实例
Dim myControl作为New SomeControl()

'设置属性和注册事件处理程序
myControl.SomeProperty = row.Cells [2] .Value
myControl.SomePropertyChanged + = myControl_SomePropertyChanged

'使其不可见
myControl.Visible = False

'设置您想要的单元格的控件
的标签row.Cells [2] .Tag = myControl

'添加控件到控件的集合
this.categoryDataGridView.Controls.Add(myControl)
下一个
结束Sub

Private Sub myControl_SomePropertyChanged(sender as object,e as EventArgs)

'SomePropertyChanged的自定义控件的事件处理程序
'这里的东西
End Sub

私有子dataGridView_CellPainting(发件人作为对象,e as DataGridViewCellPaintingEventArgs)
'为每行
对于i为Integer = 0到dataGridView.RowCount- 1
'从您想要的单元格的标签中提取控件
var myControl = this.dataGridView。行[i] .Cells [2] .Tag as SomeControl

'获取单元格矩形
Dim cellRectangle As Rectangle = dataGridView.GetCellDisplayRectangle(2,i,True)

'设置位置
myControl.Location = New Point(cellRectangle.X,cellRectangle.Y)

'设置大小
myControl.Size =新建大小(cellRectangle.Width - 1,cellRectangle.Height - 1)

'使可见
myControl.Visible = True
下一个
}
/ pre>

I made a usercontrol that is basically a panel with a bunch of labels, buttons, and Event Handlers on it. I want to be able to populate a DataGridView with these controls.

I have followed the instructions in this MSDN article, and have created a DataGridViewColumn and a DataGridViewCell class to associate with my Control.

The issue I am having is that the control is not rendering. When I run the code at the link above, It shows the date in the cell, and the user control only shows when the cell is being edited. What I'd like to have happen is that the control is always visible in the cell, similar to the behavior in a DataGridViewImageColumn.

How can I force the DataGridView to always show the control?

解决方案

The main idea:

  1. Create a user control
  2. In form load event, for each record, create an instance of user control, set values to its properties, add event handlers, set its visiblity to false and then add it to your grid Controls collection, and set the tag of your wanted cell or that row to this control.
  3. Handle CellPainting event of your grid and for each row of grid, retrieve tag of your wanted cell and convert it to your control and position it in cell bounds and make it visible. To get the position that control should be shown use GetCellDisplayRectangle method of DataGridView

Screenshot:

My sample control with name of SomeControl, contains a label and a text box and a property SomeProperty and an event SomePropertyChanges. I show cell value in TextBox of my custom control.

You can do anything with your custom control containing any control and any type of properties and events.

Code:

Here is vb-like pseudo code.

'This is not VB, this is vb-like pseudo code 
Private Sub Form_Load(sender as object , e as EventArgs)
    'Get data
    'Show data in grid

    'For each row
    For Each row as DataGridViewRow in this.categoryDataGridView.Rows
        'Create an instance of control
        Dim myControl as New SomeControl()

        'Set properties and register event handlers
        myControl.SomeProperty = row.Cells[2].Value
        myControl.SomePropertyChanged += myControl_SomePropertyChanged

        'Make it invisible
        myControl.Visible = False

        'Set tag of your wanted cell to control
        row.Cells[2].Tag = myControl

        'Add control to Controls collection of grid
        this.categoryDataGridView.Controls.Add(myControl)
    Next
End Sub

Private Sub myControl_SomePropertyChanged(sender as object, e as EventArgs)

    'event handler of SomePropertyChanged for custom control
    'do stuff here
End Sub

Private Sub dataGridView_CellPainting(sender as object, e as DataGridViewCellPaintingEventArgs )
    'for each row
    For i as Integer=0 To dataGridView.RowCount- 1
        'Extract control from tag of your wanted cell
        var myControl= this.dataGridView.Rows[i].Cells[2].Tag as SomeControl

        'Get cell rectangle
        Dim cellRectangle As Rectangle= dataGridView.GetCellDisplayRectangle(2, i, True)

        'Set location
        myControl.Location = New Point(cellRectangle.X, cellRectangle.Y )

        'Set size
        myControl.Size = New Size(cellRectangle.Width - 1, cellRectangle.Height - 1)

        'Make visible
        myControl.Visible = True
    Next
}

这篇关于如何在VB.net中添加一个UserControl到DataGridView,并且控件总是显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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