简体   繁体   English

如何在DataGridView中查找带有列索引的列名?

[英]How to find column name with column index in DataGridView?

I want to find column name in DataGridView .我想在DataGridView找到列名。 I have column index.我有列索引。 How can I find it.我怎样才能找到它。

dGVTransGrid.CurrentCell.ColumnIndex : I want it's column name. dGVTransGrid.CurrentCell.ColumnIndex :我想要它的列名。

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?可能有更好的方法,但为什么不直接询问 DataGridView 具有该索引的列叫什么?

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 br, 埃里克·埃斯特拉达·戈麦斯

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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