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.
6 Replies - 1519 Views - Last Post: 03 September 2012 - 08:47 AM
#1
How to get the column header name of a selected cell ?
Posted 20 August 2012 - 12:46 PM
Replies To: How to get the column header name of a selected cell ?
#2
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
#3
Re: How to get the column header name of a selected cell ?
Posted 20 August 2012 - 11:34 PM
Ok I have this code:
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.
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.
#4
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 ? ...
#5
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:
If you insist to get the header:
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.
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
#6
Re: How to get the column header name of a selected cell ?
Posted 03 September 2012 - 01:43 AM
Problem solved, Thank you!
#7
Re: How to get the column header name of a selected cell ?
Posted 03 September 2012 - 08:47 AM
John11, 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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|