繁体   English   中英

DataGridView单元格格式错误

[英]DataGridView Cell Formatting Error

DataGridView再次抓住了我,我无法弄清楚,为什么在以下代码中出现单元格格式化错误。 我在网上找到的所有解决方案均无法使用。 加载时我设置:

    Dim dtAlign As New DataTable  ' creating and filling a DataTable
    dtAlign.Columns.Add("ID", GetType(Int32))
    dtAlign.Columns.Add("Text", GetType(String))
    dtAlign.Rows.Add(1, "Left")
    dtAlign.Rows.Add(2, "Right")
    dtAlign.Rows.Add(3, "Center")

    Dim cola As DataGridViewComboBoxColumn = CType(Me.dgList.Columns("ColAlign"), DataGridViewComboBoxColumn)
    cola.DisplayMember = "Text"
    cola.ValueMember = "ID"
    cola.DataSource = dtAlign  ' assign datatable as a combobox col. datasource

通过VS GUI设置列(此列:名称='ColAlign',宽度= 100,DataPropertyName ='ColAlign',列类型= DataGridViewComboBoxCell等)。 它可以在将数据填充到DGV之前的一个断点工作,我看到提供了一个有效的数据表:

在此处输入图片说明

当我没有将任何数据加载到列中时,可以看到一个正确的ComboBox选择:

在此处输入图片说明

但是,如果我在此列的数据源中添加数据,则会收到单元格格式错误(说该值无效),并且该值显示为显示成员文本:

在此处输入图片说明

数据库列不可为空(此时始终为1),并且为INT。 我什至试图对其进行CAST,以确保它没有混淆,即作为字符串:

CAST(ColAlign as INT) as ColAlign

仍然,我得到了错误,并且我没有更多的想法可能出什么问题了,但是奇怪的是,数据表中的1与数据库结果集中的1不相同。 过去,我遇到了一个问题,即Int16与INT不匹配,但是Int32在数据库中始终可以正常使用INT。 乃至:

CAST(1 as INT) as ColAlign

...不起作用。 顺便说一下,我就是这样简单地分配数据的:

Me.dgList.DataSource = ds.Tables(0)

通常,它必须非常简单,这是我所缺少的。 我什至希望获得进一步调试此类问题的提示。

编辑:

我也尝试创建一个按钮,并在onClick中编写了一个代码:

Me.dgList.Rows(1).Cells("ColAlign").Value = 1

这很好。

编辑2:

我在具有固定数据类型列的数据集和数据源之间插入了一个数据表(在ComboBoxColumns的情况下为int32),这应该排除提供了错误的数据类型,但仍然无法正常工作...

                Dim dtGrid As New DataTable
                dtGrid.Columns.Add("ID", GetType(Int32))
                dtGrid.Columns.Add("FormID", GetType(Int32))
                dtGrid.Columns.Add("ColName", GetType(String
                dtGrid.Columns.Add("IsVisible", GetType(Boolean))
                dtGrid.Columns.Add("ColWidth", GetType(String))
                dtGrid.Columns.Add("ColAlign", GetType(Int32))

                dtGrid = ds.Tables(0)
                Me.dgList.AutoGenerateColumns = False
                Me.dgList.DataSource = dtGrid

编辑3

另一个调试:

    Dim dtTemp As DataTable
    Dim dgwcb As DataGridViewComboBoxColumn = CType(Me.dgList.Columns("ColAlign"), DataGridViewComboBoxColumn)
    dtTemp = CType(dgwcb.DataSource, DataTable)
    MsgBox("ValueMember name = " & dgwcb.ValueMember.ToString & vbCrLf &
           "DisplayMember name = " & dgwcb.DisplayMember.ToString & vbCrLf &
           "DataPropertyName = " & dgwcb.DataPropertyName.ToString & vbCrLf &
           "Value = " & Me.dgList.Rows(2).Cells("ColAlign").Value.ToString & vbCrLf &
           "ToString = " & dgwcb.ToString)

DataSet Visualizer中的dtTemp看起来不错,并且在MsgBox中检查的所有内容都是预期的(ValueMember =“ ID”,DisplayMember =“ Text”,DataPropertyName =“ ColAlign”,Value = 2 / I将其设置为2以进行更改/)。

解决方法:

唯一可行的方法是将ComboBoxCell的列留空,并设置DGV数据源手动填充它们,将它们的范围缩小到Int32(请注意,它们已经在SQL和dtGrid中都缩小了):

    For ir = 0 To dtGrid.Rows.Count - 1
        Me.dgList.Rows(ir).Cells("ColAlign").Value = CInt(dtGrid.Rows(ir)("ColAlign"))
    Next

由于我很少将DGV与ComboBoxCell一起使用,因此我可以接受这种“解决方案”。

编辑4 /解决方法2

我创建了第二个相同的(结构)dtGrid2,并将值逐行和逐个单元地复制到第二个中, 这与dtGrid2一起使用 ,而不与原始dtGrid一起使用! 因此,当我从数据库中提取数据时,列的原始正确数据类型被重新键入错误。 我怀疑这仅发生在SQLiteDataAdapter上,因为我过去从未在SqlDataAdapter上经历过。 简化代码说明:

    Dim dtGrid As New DataTable
    Dim dtGrid3 As New DataTable

    dtGrid.Columns.Add("ID", GetType(Int32))
    dtGrid.Columns.Add("ColName", GetType(String))
    dtGrid.Columns.Add("ColAlign", GetType(Int32))
    ' load data from DB. ...and corrupt the dtGrid...
    dtGrid = ds.Tables(0).DefaultView.ToTable  

    dtGrid3.Columns.Add("ID", GetType(Int32))          ' identical columns
    dtGrid3.Columns.Add("ColName", GetType(String))    ' identical columns
    dtGrid3.Columns.Add("ColAlign", GetType(Int32))    ' identical columns

    For ir = 0 To dtGrid.Rows.Count - 1  ' copy all values to new identical table
        dtGrid3.Rows.Add({dtGrid(ir)("ID"), dtGrid(ir)("ColName"), dtGrid(ir)("ColAlign")})
    Next

    Me.dgList.DataSource = dtGrid3  ' works!!! Doesn't with dtGrid...

尝试这个:

Dim cola As DataGridViewComboBoxColumn = CType(Me.dgList.Columns("ColAlign"), DataGridViewComboBoxColumn)
cola.DataSource = dtAlign  
cola.DataPropertyName = "ID"
cola.DisplayMember = "Text"
cola.ValueMember = "ID"

如果要绑定到DataTable之类的DataSource,则需要设置DataPropertyName

您有一个设置了一些列的DataGridView。
像这样:

在此处输入图片说明

但可能还有其他。
让我们使用List(Of Class )创建一个示例数据源:

Public Class MyDataSourceRow

    Private _ID As Integer
    Private _Text As String

    Public Sub New()

    End Sub

    Public Property ID() As Integer
        Get
            Return Me._ID
        End Get
        Set(ByVal value As Integer)
            Me._ID = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return Me._Text
        End Get
        Set(ByVal value As String)
            Me._Text = value
        End Set
    End Property

End Class


创建一个新的DataGridViewComboBoxColumn ,设置其基本属性,指定一个DataSource并将其添加到DataGridView中:

    Dim MyDataSource = New List(Of MyDataSourceRow)
    MyDataSource.Add(New MyDataSourceRow With {.ID = 1, .Text = "Left"})
    MyDataSource.Add(New MyDataSourceRow With {.ID = 2, .Text = "Right"})
    MyDataSource.Add(New MyDataSourceRow With {.ID = 3, .Text = "Center"})

    Dim MyColumn As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn
    MyColumn.Width = 150
    ' Name of the new Column. Will also be the text of the Header
    MyColumn.Name = "ComboAlignment"

    'Define its DataSource 
    MyColumn.DataSource = MyDataSource
    ' The DataPropertyName is the field of your DataSource to which your column value is bound
    MyColumn.DataPropertyName = "ID"
    ' This is the value you want the User to see
    MyColumn.DisplayMember = "Text"
    ' ValueMember is the value passed to the DataGrid when a User selects an entry in the DropDown
    MyColumn.ValueMember = "ID"

    'Insert the column in the first position -> Index(0)
    Me.DataGridView1.Columns.Insert(0, MyColumn)


现在,这是结果:

在此处输入图片说明

由于某种原因,您将列的.Name.DataPropertyName

暂无
暂无

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

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