[英]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
但可能還有其他。
讓我們使用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.