Solved

vb.net datagridview column name

Posted on 2010-11-08
6
629 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:rflorencio
  • 4
6 Comments
 
LVL 7

Expert Comment

by:rogerard
ID: 34085049
If you us this code, it will work for you:



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

Open in new window

0
 
LVL 7

Expert Comment

by:rogerard
ID: 34085115
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
 

Author Comment

by:rflorencio
ID: 34085222
rogerard, in this part of code an error appear:

IndexOf("columnname") -> value of type string cannot be converted to system.windows.forms.datagridviewcolumn
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Expert Comment

by:rogerard
ID: 34085262
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
 
LVL 7

Expert Comment

by:rogerard
ID: 34085276
Or simpler, this:

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

Open in new window

0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 34085285
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question