添加第一个行后无法将行添加到 DataGridView
我有这样的代码:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) {
db database = new db();
database.set_connection(Properties.Settings.Default.connection);
DataTable result = database.search(textBox1.Text, "", "");
if (result.Rows.Count == 0)
{
MessageBox.Show("nothing found", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.SelectAll();
textBox1.Focus();
}
else {
if (dataGridView1.Rows.Count == 0)
{
DataGridViewRow row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
row.Cells[0].Value = result.Rows[0]["title"].ToString();
row.Cells[1].Value = result.Rows[0]["code"].ToString();
row.Cells[2].Value = result.Rows[0]["sell"].ToString();
row.Cells[3].Value = "1";
row.Cells[4].Value = "";
decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
row.Cells[5].Value = total;
}
else {
bool found = false;
int position = 0;
foreach (DataGridViewRow ro in dataGridView1.Rows)
{
if (ro.Cells[0].Value.ToString() == result.Rows[0]["title"].ToString())
{
found = true;
position = ro.Index;
break;
}
}
if (found)
{
dataGridView1.Rows[position].Cells[3].Value = Convert.ToInt32(dataGridView1.Rows[position].Cells[3].Value) + 1;
decimal total = Convert.ToDecimal(dataGridView1.Rows[position].Cells[2].Value) * Convert.ToDecimal(dataGridView1.Rows[position].Cells[3].Value);
dataGridView1.Rows[position].Cells[5].Value = total;
}
else {
DataGridViewRow row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
row.Cells[0].Value = result.Rows[0]["title"].ToString();
row.Cells[1].Value = result.Rows[0]["code"].ToString();
row.Cells[2].Value = result.Rows[0]["sell"].ToString();
row.Cells[3].Value = "1";
row.Cells[4].Value = "";
decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
row.Cells[5].Value = total;
}
}
}
}
}
当我运行我的应用程序时,我可以毫无问题地添加第一行,但是当我想添加第二行时,我收到此错误:
Specified argument was out of the range of valid values.
Parameter name: rowIndex
有什么问题?
编辑:
错误发生在:
+ row.Cells[0].Value 'row.Cells[0].Value' threw an exception of type 'System.ArgumentOutOfRangeException' object {System.ArgumentOutOfRangeException}
编辑2:
这是我重写某些部分后的完整新代码,但我仍然遇到同样的问题
I have this code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) {
db database = new db();
database.set_connection(Properties.Settings.Default.connection);
DataTable result = database.search(textBox1.Text, "", "");
if (result.Rows.Count == 0)
{
MessageBox.Show("nothing found", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.SelectAll();
textBox1.Focus();
}
else {
if (dataGridView1.Rows.Count == 0)
{
DataGridViewRow row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
row.Cells[0].Value = result.Rows[0]["title"].ToString();
row.Cells[1].Value = result.Rows[0]["code"].ToString();
row.Cells[2].Value = result.Rows[0]["sell"].ToString();
row.Cells[3].Value = "1";
row.Cells[4].Value = "";
decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
row.Cells[5].Value = total;
}
else {
bool found = false;
int position = 0;
foreach (DataGridViewRow ro in dataGridView1.Rows)
{
if (ro.Cells[0].Value.ToString() == result.Rows[0]["title"].ToString())
{
found = true;
position = ro.Index;
break;
}
}
if (found)
{
dataGridView1.Rows[position].Cells[3].Value = Convert.ToInt32(dataGridView1.Rows[position].Cells[3].Value) + 1;
decimal total = Convert.ToDecimal(dataGridView1.Rows[position].Cells[2].Value) * Convert.ToDecimal(dataGridView1.Rows[position].Cells[3].Value);
dataGridView1.Rows[position].Cells[5].Value = total;
}
else {
DataGridViewRow row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
row.Cells[0].Value = result.Rows[0]["title"].ToString();
row.Cells[1].Value = result.Rows[0]["code"].ToString();
row.Cells[2].Value = result.Rows[0]["sell"].ToString();
row.Cells[3].Value = "1";
row.Cells[4].Value = "";
decimal total = Convert.ToDecimal(result.Rows[0]["sell"].ToString()) * 1;
row.Cells[5].Value = total;
}
}
}
}
}
When I run my app I can add the first row without any problems but when I want to add a second row, I got this error :
Specified argument was out of the range of valid values.
Parameter name: rowIndex
What is the problem??
EDIT:
the error is happening in:
+ row.Cells[0].Value 'row.Cells[0].Value' threw an exception of type 'System.ArgumentOutOfRangeException' object {System.ArgumentOutOfRangeException}
EDIT 2:
This is my full new code after rewriting some parts but I am still having the same problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在迭代集合时正在修改它..坏主意。无需在循环中将行添加到
DataGridView
中,只需创建一个列表并将每一行存储在列表中,并在退出循环后将它们添加到DataGridView
中即可。此外,无论
结果
是什么,您都需要在尝试对其内容建立索引之前确保它不为空。You are modifying a collection while you iterate over it..bad idea. Instead of adding the rows to the
DataGridView
in the loop, just create a list and store each row in the list and add them to theDataGridView
after you exit the loop.Also, whatever
result
is, you need to make sure that it is not empty before trying to index it's contents.在调试器中运行并在 Debug -> 中启用异常例外情况-> CLR 异常 并查看哪里会崩溃。
我只能假设
result.Rows[0] 中存在问题,
您尚未检查Rows != null 和 Rows.Count() > 是否为0
Run in debugger and enable exceptions in
Debug -> Exceptions -> CLR Exceptions
and see where it will crash.I just can assume that a problem in
result.Rows[0],
you haven't checked whetherRows != null and Rows.Count() > 0
您的 else 子句中有多个 result.row[0] 实例,我看不到您在哪里声明“结果”,因此这可能是错误的来源(如果不是错误的原因)。
You have several instances of result.row[0] in your else clause, i dont see where you are declaring 'result' so this is probably the source if not the cause of the error.
在循环遍历行的同时向表中添加行似乎是一种糟糕的方法。也许这就是问题所在。
Adding rows to a table while looping through the rows seems like a poor way to go about it. Perhaps that is causing the problem.
您只需在每个循环中将当前行设置为最后添加的行和当前单元格
格
然后设置当前单元
像这样的东西
You simply need to set the current row to the last added row and currentcell on every loop
something like
then set the currentcell
something like this