我正在循环尝试提取值的数据网格视图。我想从第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的最后一行。显然我在行数上做错了。谁能向我解释我做错了什么?
答案 0 :(得分:0)
// -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;
}
}