Hallo I have a following code , which paste the clipboard values to datagridview. I want to include a step to check if the value matches the datagridview column type then paste otherwise just skip that value. I can get the column value type but dont know how to parse the value according to column type.
Dim dgv As DataGridView = CType(dgvObj, DataGridView)
'Show Error if no cell is selected
If dgv.SelectedCells.Count = 0 Then
MessageBox.Show("Please select a cell", "Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
'Get the starting Cell
Dim startCell As DataGridViewCell = GetStartCell(dgv)
'Get the clipboard value in a dictionary
Dim cbValue As Dictionary(Of Integer, Dictionary(Of Integer, String)) = ClipBoardValues(Clipboard.GetText())
Dim iRowIndex As Integer = startCell.RowIndex
For Each rowKey As Integer In cbValue.Keys
Dim iColIndex As Integer = startCell.ColumnIndex
For Each cellKey As Integer In cbValue(rowKey).Keys
'Check if the index is within the limit
If iColIndex <= dgv.Columns.Count - 1 AndAlso iRowIndex <= dgv.Rows.Count - 1 Then
Dim cell As DataGridViewCell = dgv(iColIndex, iRowIndex)
Dim Valtype As Type = dgv.Columns(iColIndex).ValueType
If Not cell.ReadOnly Then
TryCast(cbValue(rowKey)(cellKey), Valtype)------------------check or convert value type
cell.Value = cbValue(rowKey)(cellKey)
End If
End If
iColIndex += 1
Next
iRowIndex += 1
Next