簡體   English   中英

通過引用列名查找列索引?

[英]Find Column Index by referencing Column Name?

如何從DataGridView通過ColumnName獲取ColumnColumnIndex

這是偽代碼:

ColumnIndex = ColumnName("SampleName"); 

您可以使用IndexOf 像這樣:

var dataGridViewColumn = dataGridView1.Columns[ColumnName];
if (dataGridViewColumn != null)
{
    int index = dataGridView1.Columns.IndexOf(dataGridViewColumn);
}

或像這樣使用IndexNull條件運算符( ?. ):

var index = dataGridView1.Columns[ColumnName]?.Index;

獲取列的索引

myDGV.Columns.IndexOf(myDGV.Columns["SampleColumn"];

或者更像你說的

myDGV.Columns["SampleColumn"].Index;

最簡單的方法是:

myData.Columns.IndexOf(/*DataGridViewColumn*/)
myData.Columns[/*ColumnName*/].Index

要么:

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int ColumnIndex= dataGridView.CurrentCell.ColumnIndex;       
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM