首页 文章

DataGridView单元格格式错误

提问于
浏览
0

DataGridView再次 grab 了我,我想不通,为什么我用以下代码得到单元格格式错误 . 我在网上找到的解决方案都不会起作用/适合 . 在加载时我设置:

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设置(此列:Name ='ColAlign',Width = 100,DataPropertyName ='ColAlign',列类型= DataGridViewComboBoxCell等) . 它只是在将数据填充到DGV之前的断点处工作,我看到提供了一个有效的数据表:

enter image description here

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

enter image description here

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

enter image description here

数据库列不可为空(此时始终为1),并且为INT . 我甚至尝试过CAST它,以确保它没有混淆,即作为字符串:

CAST(ColAlign as INT) as ColAlign

仍然,我得到错误,我没有更多的想法可能是错的,但是在数据表中的1与数据库结果集中的1不同 . 在过去我遇到了Int16不匹配INT的问题,但Int32总是在数据库中使用agaist INT . 乃至:

CAST(1 as INT) as ColAlign

......不起作用 . 顺便说一句,我这样简单地分配数据:

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

通常,它必须是非常简单的东西,我缺少的东西 . 我甚至会提示如何进一步调试这样的问题 .

EDIT:

我也试过创建一个按钮并在onClick上写了一个代码:

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

这很好用 .

EDIT 2:

我在DataSet和DataSource之间使用固定数据类型列(在ComboBoxColumns的情况下为int32)插入了一个DataTable,它应该排除提供错误的数据类型并且它仍然不起作用...

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

EDIT 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”,值= 2 /我将其设置为2以进行更改/) .

WORKAROUND:

它的唯一工作方式是将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

由于我很少使用带有ComboBoxCell的DGV,我可以使用这个“解决方案” .

EDIT 4 / WORKAROUND 2

我创建了第二个,相同的(结构)dtGrid2并逐行复制值并逐个单元格复制到第二个并且在过去使用SqlDataAdapter并且没有经历过它 . [1399485]并且没有经历过它 . 一个简化的代码来解释:

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...

2 回答

  • 0

    试试这个:

    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

  • 0

    您有一个DataGridView,其中设置了一些列 .
    像这样的东西:

    enter image description here

    但可能是其他任何东西 .
    让我们使用List(Of Class)创建一个示例DataSource:

    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
    

    Creeate一个新的 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)
    

    现在,这是结果:

    enter image description here

    出于某种原因,你混淆列的 .Name 与它的 .DataPropertyName

相关问题