6 Replies - 6115 Views - Last Post: 03 September 2012 - 08:47 AM Rate Topic: -----

#1 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

How to get the column header name of a selected cell ?

Posted 20 August 2012 - 12:46 PM

Hello, I'm using VB.NET 2010. I want to know if there's any way I could get the column header text of a selected cell.
For example, if I have a gridview with 3 columns (Mac ID, Credit IN, Credit OUT), if I click on any cell in the column Credit IN, I want a msgbox to appear saying 'Column Credit IN is chosen'.
Same should happen for Mac ID and Credit OUT.

Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: How to get the column header name of a selected cell ?

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: How to get the column header name of a selected cell ?

Posted 20 August 2012 - 03:27 PM

have you tried anything yet? if so lets see what you have done so far
Was This Post Helpful? 0
  • +
  • -

#3 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: How to get the column header name of a selected cell ?

Posted 20 August 2012 - 11:34 PM

Ok I have this code:

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
                Number = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
    End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim f As New frmUpdate
        f.ShowDialog()
    End Sub

Private Sub frmUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtNumber.Text = Number
        txtNumber.Text = FormatCurrency(txtNumber.Text)
    End Sub

 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim con As New SqlClient.SqlConnection
        con.ConnectionString = My.Settings.ACMA_DB_ConnectionString
        Dim query As String
        query = "Update Logs Set CreditIN = '" & ReadSumIN & "', CreditOUT = '" & ReadSumOUT & "' Where Mac_ID = '" &                          Mac_ID & "' AND Date_ID Between '" & DatePick2 & "' AND '" & DatePick3 & "'"
        Dim cmdinsert As New SqlCommand
        cmdinsert.CommandText = query
        cmdinsert.Connection = con
        con.Open()
        cmdinsert.ExecuteNonQuery()
        con.Close()

        txtNumber.Text = ""
    End Sub



The Last button (btnUpdate) must update according to the column header that means if I click on a cell contained in a column named 'Credit IN', I must update only CreditIN.
The same must happen for columns Mac_ID and CreditOUT.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: How to get the column header name of a selected cell ?

Posted 29 August 2012 - 03:03 AM

No reply yet ? is there anything unclear ? ...
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is online

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to get the column header name of a selected cell ?

Posted 29 August 2012 - 05:09 AM

why dont you use column index instead of header if the user cant move the columns. the index is integer and its always better to compare integers instead of text. for example:
        For Each c As DataGridViewCell In DataGridView1.SelectedCells
            MessageBox.Show(c.ColumnIndex().ToString())
        Next



If you insist to get the header:

        For Each c As DataGridViewCell In DataGridView1.SelectedCells
            MessageBox.Show(c.ColumnIndex().ToString())
            MessageBox.Show(DataGridView1.Columns(c.ColumnIndex).HeaderText)
        Next        



now that you know how to get the column header and index just make a string according to the column header/index.

good luck

EDIT:
also you better get the column name instead of the column header. if you make your program to have several languages the header will be different for different languages. instead of using .HeaderText use .Name property.

This post has been edited by raziel_: 29 August 2012 - 05:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: How to get the column header name of a selected cell ?

Posted 03 September 2012 - 01:43 AM

Problem solved, Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to get the column header name of a selected cell ?

Posted 03 September 2012 - 08:47 AM

View PostJohn11, on 29 August 2012 - 05:03 AM, said:

No reply yet ? is there anything unclear ? ...

Please don't do that! Give people time to respond. You took 8 hours to respond to wylie's post, and bumped 3 hours later. Also, you're halfway around the world from the US, and two hours later than the UK. Please keep in mind that you're often posting in the middle of the night to others.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1