C# 基于CurrentRow查询特定DataGridView列
我在C#(VisualStudio2010)中使用了一个.NET4.5DataGridView,它有几个列(索引、PosX、PosY等)。我用几行自定义数据填充它,如下所示:C# 基于CurrentRow查询特定DataGridView列,c#,windows,datagridview,C#,Windows,Datagridview,我在C#(VisualStudio2010)中使用了一个.NET4.5DataGridView,它有几个列(索引、PosX、PosY等)。我用几行自定义数据填充它,如下所示: string[] row = new string[MyDataGridView.ColumnCount]; for (int i = 0; i < NumResults; i++) row = new string[] { i.ToString(), PosXArray[i].ToString(), Pos
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);
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;
}
string ObjectIndex = MyDataGridView.Rows[MyDataGridView.CurrentRow.Index].Cells[0].Value.ToString();
只要CurrentRow索引与第1列上的“index”值相同,这种方法就有效。但是,用户可以以不同的方式对数据网格进行排序,例如基于“位置X”列
因此,我想知道如何仅基于行索引访问特定列的值 我找到了这样的解决方案:
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);
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;
}
string ObjectIndex = MyDataGridView.Rows[MyDataGridView.CurrentRow.Index].Cells[0].Value.ToString();