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设置列(此列:Name ='ColAlign',Width = 100,DataPropertyName ='ColAlign',列类型= DataGridViewComboBoxCell等)。 它在将数据填充到DGV之前的断点处起作用,我看到提供了一个有效的数据表:
当我不将任何数据加载到列中时,我可以看到正确的ComboBox选择:
但是,如果我在此列的数据源中添加数据,则会出现单元格格式错误(表示该值无效),并且该值显示为显示成员文本:
数据库列不可为空(此时始终为1),并且为INT。我甚至尝试过它,以确保它没有混淆,即字符串:
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)
通常,它必须是非常简单的东西,我缺少的东西。我甚至会想到如何进一步调试这样的问题。
修改
我也试过创建一个按钮,并在onClick中编写了一个代码:
Me.dgList.Rows(1).Cells("ColAlign").Value = 1
这很有效。
编辑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
编辑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以进行更改/)
解决方法:
它的唯一工作方式是将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...
答案 0 :(得分:0)
您有一个DataGridView,其中设置了一些列
这样的事情:
但可能是其他任何东西。
让我们使用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
蠕动一个新的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