循环从DataGridView1获得最后一行的值

本文关键字:一行 DataGridView1 最后 循环 | 更新日期: 2023-09-27 17:51:14

我正在循环通过一个数据网格视图试图提取值。我想从列1 中的值开始,用代替列0中的

在下面的代码中…

for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
{
    for (int col = 1; col < dataGridView1.Rows[rows].Cells.Count; col--)
    {
        string dataGridValue = dataGridView1.Rows[rows].Cells[col].Value.ToString();
        MessageBox.Show(dataGridValue);
        if (col == 0)
        {
            col = col + 2;
            if (rows < dataGridView1.Rows[rows].Cells.Count) {
                rows = rows + 1;
            }
            else
            {
                rows = dataGridView1.RowCount - 1;
            }
        }
    }
}

它一直获取dataGridView的最后一行。显然,我在行数上做错了什么。谁能告诉我我哪里做错了?

循环从DataGridView1获得最后一行的值

// -1 because you want to start at column 1
var extractedValues = new object[dataGridView1.RowCount, dataGridView1.ColumnCount - 1];  
for (int row = 0; row < dataGridView1.RowCount; row++)
{
  for (int col = 1; col < dataGridView1.ColumnCount; col++)
  {
    extractedValues[row, col -1] = dataGridView1.Rows[rows].Cells[col].Value;
  }
}