I want to find column name in DataGridView
.
I have column index. How can I find it.
dGVTransGrid.CurrentCell.ColumnIndex
: I want it's column name.
Plz help.
Thanks.
I want to find column name in DataGridView
.
I have column index. How can I find it.
dGVTransGrid.CurrentCell.ColumnIndex
: I want it's column name.
Plz help.
Thanks.
There may be a better way, but why don't you just ask the DataGridView what the column with that index is called?
int columnIndex = dGVTransGrid.CurrentCell.ColumnIndex;
string columnName = dGVTransGrid.Columns[columnIndex].Name;
Found an easier way, on a single line.
[datagrid].CurrentCell.OwningColumn.Name
hope it helps.
br, Eric Estrada Gomez
If you want it dynamic on the cell clicked then you can get it from the event
private void dataGridViewName_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
String columnName = this.dataGridViewName.Columns[e.ColumnIndex].Name;
}
for (var i = 0; i < DataGridView.ColumnCount; i++)
var name = DataGridView.Columns[i].HeaderText;
this is a simple way of doing it.
In a single row of code
String name = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name;
I think I'm quite late to answer this, I was going through the same requirement and I found this query. I tried the below code snippet and it worked for me:
Since you said you know index, you can use the below snippet. If you already have an index number, then you can directly fetch the column name based on that index.
Dim index As Integer = 0
Dim grid As DataGridView
Dim colName As String = grid.ColumnFields(index).HeaderText
If you don't know the index number, you can use the below snippet.
Dim colIndex As Integer = 0
Dim colName As String = ""
Dim grid As DataGridView
For index As Integer = 0 To grid.ColumnFields.Length - 1
If grid.ColumnFields(index).HeaderText = colName Then
colIndex = index
Exit For
End If
Next
It may be helps you:
String s = dataGridView1.Columns[Index].HeaderText;