簡體   English   中英

循環以從DataGridView1獲取值並卡在最后一行

[英]Loop to get values from DataGridView1 getting stuck on last row

我在遍歷一個datagrid視圖試圖提取值。 我想從第一 而不是零列開始。

在下面的代碼中..

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的最后一行。 顯然我在行計數方面做錯了。 誰能向我解釋我做錯了什么?

// -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;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM