vb.net datagridview column name

Private Sub DgvDetalhe_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgvDetalhe.CellClick

If e.ColumnIndex = 2 Then
 .......
endif

Is possible replace column index by column name, something like if e.columnindex="column1" then
rflorencioAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gamarrojgqConnect With a Mentor Commented:
Hi,

You can user the e.columnindex to know the NAME of the column like this

        If DataGridView1.Columns(e.ColumnIndex).Name = "column1" Then
            ...
        End If

The name will be the one you assigned in he column properties
0
 
rogerardCommented:
If you us this code, it will work for you:



if e.columnindex = Directcast(sender, DataGridView).Columns.IndexOf("columnname"))

Open in new window

0
 
rogerardCommented:
Sorry had too many ) and omitted then and end if-- see revised code here:

If e.ColumnIndex = DirectCast(sender, DataGridView).Columns.IndexOf("columnname") Then
....
End If

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
rflorencioAuthor Commented:
rogerard, in this part of code an error appear:

IndexOf("columnname") -> value of type string cannot be converted to system.windows.forms.datagridviewcolumn
0
 
rogerardCommented:
Sorry.  Try this:



If e.ColumnIndex = DirectCast(sender, DataGridView).Columns.IndexOf(DirectCast(sender, DataGridView).Columns.Item("columnname")) Then
....
    End If

Open in new window

0
 
rogerardCommented:
Or simpler, this:

    If e.ColumnIndex = DirectCast(sender, DataGridView).Columns.Item("columnname").Index Then
....
    End If

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.