数据网格迭代错误。当 null 时退出 For 循环?
Data grid iteration error. Exit For loop when null?
我对编程还比较陌生,所以放轻松!
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
如何防止在遍历 datagridview 后发生这些错误。该代码完全满足需要。 for 循环遍历了所有行,但在没有剩余行时抛出异常。当所有行都已转换时,我该如何突破?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
//String header = gridview_id.Columns[i].HeaderText;
// String cellText = row.Cells[i].Text;
partData.partID = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
partData.productName = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
partData.partDescription = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
partData.unitPrice = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
partData.quantity = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
partData.partNote = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dmcalla04.students.qub.ac.uk/import.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
}
非常感谢。
所以基本上你需要使用 row
变量而不是 dataGridView1.Rows[i]
你退出参数异常的原因是在行数小于行数的情况下自 dataGridView1.Rows[i]
行尝试访问不存在的数组索引后收到的列。
foreach (var row in dataGridView1.Rows)
{
partData.partID = Convert.ToString(row.Cells[0].Value);
partData.productName = Convert.ToString(row.Cells[1].Value);
partData.partDescription = Convert.ToString(row.Cells[2].Value);
partData.unitPrice = Convert.ToString(row.Cells[3].Value);
partData.quantity = Convert.ToString(row.Cells[4].Value);
partData.partNote = Convert.ToString(row.Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
或者如果你想使用索引,你可以使用下面的代码:
for (int rowIndex = 0; rowIndex < dataGrid.Rows.Count; rowIndex++)
{
var row = dataGridView1.Rows[rowIndex];
partData.partID = Convert.ToString(row.Cells[0].Value);
partData.productName = Convert.ToString(row.Cells[1].Value);
partData.partDescription = Convert.ToString(row.Cells[2].Value);
partData.unitPrice = Convert.ToString(row.Cells[3].Value);
partData.quantity = Convert.ToString(row.Cells[4].Value);
partData.partNote = Convert.ToString(row.Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
您可能还想查看 this answer
院长,
当您尝试引用 DataGridView 中不存在的行或列时,将导致 'out of range' 错误。在您的情况下,您的计数器 (i) 重复 Columns 的数量,但您的代码引用了 DGV 的 行。修复该错误应该可以解决您的问题。
回复您的评论:
替换行:
for (int i = 0; i < dataGridView1.Columns.Count; i++)
和
for (int i = 0; i < dataGridView1.Rows.Count; i++)
应该可以防止错误,但是如果不知道您要完成的确切目标,就很难进一步指导您。你那里有一个嵌套循环,据我所见,其中有很多是多余的。
我对编程还比较陌生,所以放轻松!
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
如何防止在遍历 datagridview 后发生这些错误。该代码完全满足需要。 for 循环遍历了所有行,但在没有剩余行时抛出异常。当所有行都已转换时,我该如何突破?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
//String header = gridview_id.Columns[i].HeaderText;
// String cellText = row.Cells[i].Text;
partData.partID = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
partData.productName = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
partData.partDescription = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
partData.unitPrice = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
partData.quantity = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
partData.partNote = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dmcalla04.students.qub.ac.uk/import.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
}
非常感谢。
所以基本上你需要使用 row
变量而不是 dataGridView1.Rows[i]
你退出参数异常的原因是在行数小于行数的情况下自 dataGridView1.Rows[i]
行尝试访问不存在的数组索引后收到的列。
foreach (var row in dataGridView1.Rows)
{
partData.partID = Convert.ToString(row.Cells[0].Value);
partData.productName = Convert.ToString(row.Cells[1].Value);
partData.partDescription = Convert.ToString(row.Cells[2].Value);
partData.unitPrice = Convert.ToString(row.Cells[3].Value);
partData.quantity = Convert.ToString(row.Cells[4].Value);
partData.partNote = Convert.ToString(row.Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
或者如果你想使用索引,你可以使用下面的代码:
for (int rowIndex = 0; rowIndex < dataGrid.Rows.Count; rowIndex++)
{
var row = dataGridView1.Rows[rowIndex];
partData.partID = Convert.ToString(row.Cells[0].Value);
partData.productName = Convert.ToString(row.Cells[1].Value);
partData.partDescription = Convert.ToString(row.Cells[2].Value);
partData.unitPrice = Convert.ToString(row.Cells[3].Value);
partData.quantity = Convert.ToString(row.Cells[4].Value);
partData.partNote = Convert.ToString(row.Cells[5].Value);
MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
Console.WriteLine(username);
}
您可能还想查看 this answer
院长, 当您尝试引用 DataGridView 中不存在的行或列时,将导致 'out of range' 错误。在您的情况下,您的计数器 (i) 重复 Columns 的数量,但您的代码引用了 DGV 的 行。修复该错误应该可以解决您的问题。
回复您的评论: 替换行:
for (int i = 0; i < dataGridView1.Columns.Count; i++)
和
for (int i = 0; i < dataGridView1.Rows.Count; i++)
应该可以防止错误,但是如果不知道您要完成的确切目标,就很难进一步指导您。你那里有一个嵌套循环,据我所见,其中有很多是多余的。