DataGridView中Combobox的資料類型比對
假如Combobox中設定的時候的資料類型時int32,DataGridView顯示是比對的資料是double型的,顯示時就會報錯。
解決方法:
1。比對時作判斷(MaxID是要比對的資料,預設是int32)
If dgvMaster.Rows(0).Cells(0).Value.GetType.Name = "Double" Then
dgvMaster.Rows.Add(CType(MaxID, Double), "")
dgvMaster.Rows(dgvMaster.Rows.Count - 1).Cells(j).Value = CType(MaxOrder, Double)
ElseIf dgvMaster.Rows(0).Cells(0).Value.GetType.Name = "Short" Then
dgvMaster.Rows.Add(CType(MaxID, Short), "")
dgvMaster.Rows(dgvMaster.Rows.Count - 1).Cells(j).Value = CType(MaxOrder, Short)
Else
dgvMaster.Rows.Add(MaxID, "")
dgvMaster.Rows(dgvMaster.Rows.Count - 1).Cells(j).Value = MaxOrder
End If
2。根據要比對的資料類型進行設定
Call SetGridComboBoxEx(dgv_3, "SELECT CAST(COD AS NUMBER(3,0)) AS COD,NAM FROM MZZT001 WHERE KBN = 'X0162' ORDER BY DSP_SEQ", "COD", "NAM", "方向", "System.Int16")
dgvContainer :DataGridView名字
strSQL :Combobox中設定資料的查詢
strValueMember :sql查詢中,最為資料的字段
strDispMember :sql查詢中,最為顯示的字段
strColumnName :DataGridView中,顯示為Combobox的列
strType :要設定的資料類型
Public Shared Function SetGridComboBoxEx(ByVal dgvContainer As Windows.Forms.DataGridView, ByVal strSQL As String, ByVal strValueMember As String, ByVal strDispMember As String, ByVal strColumnName As String, ByVal strType As String) As Boolean
Dim tblDataSource As DataTable
Dim dgcmb As New Windows.Forms.DataGridViewComboBoxColumn
Dim bFlag As Boolean = False
Try
'取得列的Combobox對象
For i As Integer = 0 To dgvContainer.Columns.Count - 1
If dgvContainer.Columns(i).HeaderText = strColumnName Then
dgcmb = dgvContainer.Columns(i)
bFlag = True
Exit For
End If
Next
If bFlag = False Then
Return False
End If
tblDataSource = clsDBBase.dataTable(strSQL)
If tblDataSource Is Nothing Then
tblDataSource = New DataTable
tblDataSource.Columns.Add(strValueMember, System.Type.GetType(strType))
tblDataSource.Columns.Add(strDispMember)
End If
tblDataSource.Rows.InsertAt(tblDataSource.NewRow(), 0)
tblDataSource.Rows(0).Item(strValueMember) = 0
tblDataSource.Rows(0).Item(strDispMember) = DBNull.Value
'資料綁定
dgcmb.DisplayMember = strDispMember
dgcmb.ValueMember = strValueMember
dgcmb.DataSource = tblDataSource
Return True
Catch ex As Exception
Return False
End Try
End Function
3。CellParsing事件(假設應該是Double型)
Private Sub dgvMaster_CellParsing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellParsingEventArgs) Handles dgvMaster.CellParsing
Try
If (e.Value Is Nothing) Or (e.Value Is DBNull.Value) Or (e.Value = "") Then
e.Value = Convert.ToDouble("0")
Else
e.Value = Convert.ToDouble(e.Value)
End If
e.ParsingApplied = True
End If
Catch ex As Exception
End Try
End Sub
4。DataError事件(适用于原來有此combobox選項,後來被删除了,但是,以前的資料中,combobox可能還有此選項紀錄遺留)
Private Sub dgvMaster_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvMaster.CellValidating
Dim dgvTem As DataGridView = sender
Try
dgvTem.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = ""
Catch ex As Exception
End Try
End Sub
Private Sub DataGridView_DataError(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgvMaster.DataError
Try
Call DataGridViewComboBoxCellDataErrorHandler(sender, e)
Catch ex As Exception
End Try
End Sub
Public Shared Sub DataGridViewComboBoxCellDataErrorHandler(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs)
Try
Dim lDgv As Windows.Forms.DataGridView = sender
If lDgv.Columns(e.ColumnIndex).CellType.Name = "DataGridViewComboBoxCell" Then
If lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType.Equals(Type.GetType("System.String")) Then
If IsNumeric(lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString) Then
lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = CInt(lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
End If
Else
lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
End If
lDgv.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = "此項已删除"
Else
e.ThrowException = True
End If
Catch ex As Exception
End Try
End Sub