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
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
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
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 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.
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.