none
请教一个关于DataGridView中的DataGridViewCoboBoxColumn值的显示问题 RRS feed

  • 问题

  • 如果在代码中给DataGridView中的DataGridViewCoboBoxColumn赋的值在列表中不存在,会触发DataError事件。如果DataError事件中作了处理,则可以不报Error,但是在代码中赋的值并没有显示出来,显示的是列表中的第一个值。在现在想要显示在代码中赋的值给怎么做呢?俺想到的是在Paint事件中处理,可行吗?
    请多指教:)
    2008年4月23日 0:51

全部回复


  • 太感谢了!
    我最后是这么实现的:)

    Imports System.ComponentModel
    Imports System.ComponentModel.Design
    Imports System.Windows.Forms
     
    Public Class MyComboBoxColumn
        Inherits DataGridViewComboBoxColumn
        Public Sub New()
            Me.CellTemplate = New MyComboBoxCell
        End Sub
     
    End Class

    Public Class MyComboBoxCell
        Inherits DataGridViewComboBoxCell

        Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object

            If rowIndex >= 0 AndAlso value IsNot Nothing Then

                Dim haveValue As Boolean = False

                Try
                    Dim column As DataGridViewComboBoxColumn = CType(Me.OwningColumn, DataGridViewComboBoxColumn) 'セルを格納している列

                    If column.DataSource IsNot Nothing Then

                        Dim table As DataTable = CType(column.DataSource, DataTable)
                        Dim dispalyMember As String = column.DisplayMember
                        Dim valueMember As String = column.ValueMember

                        For Each row As DataRow In table.Rows

                            If row(valueMember).ToString = value.ToString Then

                                haveValue = True
                                Exit For
                            End If
                        Next
                    ElseIf Me.DataSource IsNot Nothing Then

                        Dim table As DataTable = CType(Me.DataSource, DataTable)
                        Dim dispalyMember As String = Me.DisplayMember
                        Dim valueMember As String = Me.ValueMember

                        For Each row As DataRow In table.Rows

                            If row(valueMember).ToString = value.ToString Then

                                haveValue = True
                                Exit For
                            End If
                        Next
                    ElseIf Me.Items.IndexOf(value) >= 0 Then

                        haveValue = True
                    End If
                Catch ex As Exception

                    haveValue = True
                End Try

                If Not haveValue Then
                    Return value.ToString
                Else
                    Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
                End If
            Else

                Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
            End If

        End Function
     
    End Class
    2008年4月25日 8:54