【问题标题】:Inquiring a specific DataGridView column based on CurrentRow根据 CurrentRow 查询特定的 DataGridView 列
【发布时间】:2014-09-18 10:03:59
【问题描述】:

我在 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 列上的“索引”值相同,此方法就可以工作。但是,用户可以以不同的方式对数据网格进行排序,例如基于“位置 X”列。

所以我想知道如何仅根据行索引访问特定列的值。

【问题讨论】:

    标签: c# windows datagridview


    【解决方案1】:

    我找到了这样的解决方案:

    string ObjectIndex = MyDataGridView.Rows[MyDataGridView.CurrentRow.Index].Cells[0].Value.ToString();
    

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2023-03-12
      • 2012-02-07
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      相关资源
      最近更新 更多