[英]Find Column Index by referencing Column Name?
How to get ColumnIndex
of a Column
by ColumnName
from a DataGridView
? 如何从
DataGridView
通过ColumnName
获取Column
的ColumnIndex
?
Here's the pseudo-code: 这是伪代码:
ColumnIndex = ColumnName("SampleName");
You can use IndexOf
. 您可以使用
IndexOf
。 Like this: 像这样:
var dataGridViewColumn = dataGridView1.Columns[ColumnName];
if (dataGridViewColumn != null)
{
int index = dataGridView1.Columns.IndexOf(dataGridViewColumn);
}
Or use Index
and Null-conditional operator( ?.
) like this: 或像这样使用
Index
和Null条件运算符( ?.
):
var index = dataGridView1.Columns[ColumnName]?.Index;
To get the index of a column 获取列的索引
myDGV.Columns.IndexOf(myDGV.Columns["SampleColumn"];
or more simply like you said 或者更像你说的
myDGV.Columns["SampleColumn"].Index;
The simplest way to do that: 最简单的方法是:
myData.Columns.IndexOf(/*DataGridViewColumn*/)
myData.Columns[/*ColumnName*/].Index
Or: 要么:
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int ColumnIndex= dataGridView.CurrentCell.ColumnIndex;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.