我在C#(Visual Studio 2010)中使用.NET 4.5 DataGridView,其中包含多个列(Index,PosX,PosY,...)。我用几行自定义数据填充它:
string[] row = new string[MyDataGridView.ColumnCount];
for (int i = 0; i < NumResults; i++)
row = new string[] { i.ToString(), PosXArray[i].ToString(), PosYArray[i].ToString(), ... }
MyDataGridView.Rows.Add(row);
每行包含一个对象的信息,由“索引”列值标识。当用户选择一行时我想知道选择了哪个对象。我正在使用“SelectionChanged”事件来请求CurrentRow:
int CurrentRow = -1;
private void MyDataGridView_SelectionChanged(object sender, EventArgs e)
{
if (MyDataGridView.CurrentRow.Index != CurrentRow)
// Access the data over MyDataGridView.CurrentRow.Index
CurrentRow = MyDataGridView.CurrentRow.Index;
}
只要CurrentRow索引与第1列上的“Index”值相同,这就可以工作。但是,用户可以以不同的方式对数据网格进行排序,例如基于“位置X”列。
所以我想知道如何仅根据行索引访问特定列的值。
答案 0 :(得分:0)
我找到了这样的解决方案:
string ObjectIndex = MyDataGridView.Rows[MyDataGridView.CurrentRow.Index].Cells[0].Value.ToString();