自定义控件datagridview [英] Custom control datagridview

查看:182
本文介绍了自定义控件datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我搜索了一些说明,以创建自定义选中的组合框.然后,将其托管到DataGridViewComboBoxColumn中.

我在设计时创建了自定义选中的组合框.在表单加载时将其添加到datagridview中.单击该单元格后,我使用CellBeginEdit()将自定义控件设置到该单元格中.然后,从数据库中填充数据.

Hi All,

I''ve searched links that have some instructions to create a custom checked combobox. Then, I host it into a DataGridViewComboBoxColumn.

I created the custom checked combobox in design time. Added it to the datagridview on form load. Upon clicking the cell, I use CellBeginEdit() to set the custom control into the cell. Then, populate data from database.

Private Sub dataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs)
  If e.ColumnIndex = 1 Then
    'set location and size of checkedlistbox1.  
    Dim rec As Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)

    customCheckedCombo1.Location = rec.Location
    customCheckedCombo1.Visible = True
         
    Call FillColorList()

  End If
End Sub



第一行一切正常.但是,当我单击第二行上的自定义选中的组合框时,第一行的选中值跳到第二行,如果再次单击第一行,则相反.添加更多的行,我看到我只能有1个检查值根据单击的行来跳跃.

我不知道为什么.赞赏有人可以提供帮助.提前谢谢.

我尝试过的事情:

DataGridViewComboBoxColumn,DataGridViewComboBoxCell



Everything works fine on first row. But, when I click the custom checked combobox on the second row, the checked values from first row jumps to 2nd row, and vice-versa if I click the first row again. Adding more rows, I see I can only have 1 check value jumping according which row is clicked.

I cannot figure out why. Appreciate if someone can help. Thanks in advance.

What I have tried:

DataGridViewComboBoxColumn, DataGridViewComboBoxCell

推荐答案

您好,成员8599340,

要解决此问题,您需要创建两个类,这两个类将一起使您可以在DataGridView中使用自定义组合框.第一个类应派生 DataGridViewComboBoxCell 类,第二个类应派生 DataGridViewComboBoxColumn 类.

假设您的自定义ComboBox是在名为MyCBO的类中实现的,那么这两个类可能看起来像这样:
Hi there member 8599340,

To fix this, you need to create two classes which together will make it possible to use your custom Combobox in a DataGridView. The first class should derive the DataGridViewComboBoxCell class, and the other should derive the DataGridViewComboBoxColumn class.

Suppose your custom ComboBox is implemented in a class named MyCBO, these two classes might look like this:
' This is the class that represents your cell which can use your ComboBox class
Public Class MyDataGridViewComboBoxCell
    Inherits DataGridViewComboBoxCell

    Public Sub New()
        MyBase.New()
    End Sub

    ' You must override the EditType property to return the cell's 
    ' editing control type, which is your custom ComboBox class...
    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(MyCBO)
        End Get
    End Property

    ' You must also override this method to initialize the ComboBox instance...
    ' This method will be called each time a cell in the column enters edit-mode, 
    ' so you can fill the ComboBox instance based on the value of the edited cell
    Public Overrides Sub InitializeEditingControl(_
        ByVal rowIndex As Integer, _
        ByVal formattedValue As Object, _
        ByVal cellStyle As DataGridViewCellStyle)

        ' Call base...
        MyBase.InitializeEditingControl(rowIndex, formattedValue, cellStyle)

        ' Convert the cell's EditingControl to your custom ComboBox type...
        Dim ctl As MyCBO = CType(DataGridView.EditingControl, MyCBO)

        ' Make sure you have an instance...
        If ctl IsNot Nothing Then
            ' Populate the ComboBox, passing the instance as a parameter
            FillColorList(ctl)

            ' Set the value of the editing control instance to the current cell value.
            ctl.SelectedValue = formattedValue
        End If
    End Sub
End Class

' This is the class that represents your column which can use your cell class
Public Class MyDataGridViewComboBoxColumn
    Inherits DataGridViewComboBoxColumn

    Public Sub New()
        MyBase.New()

        ' Specify the column to use your custom cell class...
        MyBase.CellTemplate = New MyDataGridViewComboBoxCell()
    End Sub

End Class



真的就是全部!



And that''s really all there is to it!


这篇关于自定义控件datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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