简体   繁体   中英

How to find column name with column index in DataGridView?

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.

在一行代码中

String name = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name;

它可能会帮助您:

String s = dataGridView1.Columns[Index].HeaderText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM