Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于CurrentRow查询特定DataGridView列_C#_Windows_Datagridview - Fatal编程技术网

C# 基于CurrentRow查询特定DataGridView列

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

我在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(), 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();