Reputation: 71
(sorry for weakness in english)
I am working with-
window application, VB, VS 2012, .net F/W- 4.5
I have a DGV (datagridview) in form.
There are different types of column in dgv which are created runtime.
To execute next step of process first of all I have to identify which type of cell/column is clicked (like dgv-combobox, dgv-textbox etc).
Code is here, which is not working for me, So I tried to check type of clicked dgv-cell using MsgBox.
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
Dim column_type As Object
column_type = dgv.Columns(dgv.SelectedCells(0).ColumnIndex).CellType
column_type.GetType()
If TypeOf column_type Is DataGridViewComboBoxCell Then
'code goes here
MsgBox("yes")
Else
'code goes here
MsgBox(column_type.ToString)
End If
End Sub
But the problem is, control is going into the else part of the if...else statement
all the time and MsgBox(column_type.ToString)
is displaying for all types of column which are System.Windows.Forms.DataGridViewTextBoxCell or System.Windows.Forms.DataGridViewComboBoxCell.
I tried to check column type using
DataGridViewComboBoxCell,
DataGridViewComboBoxColumn,
DataGridViewComboBoxEditingControl - but nothing works.
I m not sure, but I think problem is with Dim column_type As Object
.
Please help me guys. Thanks in advance.
Upvotes: 0
Views: 1273
Reputation: 1183
Turn on Option Strict on if you haven't already (project properties > Compile)
If TypeOf DataGridView1.Columns(0) Is DataGridViewTextBoxColumn Then
MsgBox("yes")
End If
Works fine for me.
Alternative:
If DataGridView1.Columns(0).GetType Is GetType(DataGridViewTextBoxColumn) Then
MsgBox("yes")
End If
You are currently comparing typeof type to type. If it doesn't work for some reason, you should inspect the code with breakpoint.
Upvotes: 1
Reputation: 15774
The code TypeOf column_type
is redundant because column_type is actually a type! When you assign it to an Object, that's kind of wrong. You could just assign it as a Type. But even easier is to let the compiler do the thinking for you and use implicit typing
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
' cursor on Dim, it is Type
Dim column_type = dgv.Columns(dgv.SelectedCells(0).ColumnIndex).CellType
' the proper syntax is Type is GetType(Type)
If column_type Is GetType(System.Windows.Forms.DataGridViewComboBoxCell) Then
MsgBox("yes")
Else
MsgBox(column_type.ToString)
End If
End Sub
The syntax is If Type is GetType(Type) Then
See https://stackoverflow.com/a/6580236/832052
Upvotes: 1