I wrote a program that would display the information of a car when you input the cardnum. made it so that when you input another cardnum it will show the data for that car in another row on the datagridview. now I also made it that when you input a cardnum for a car thats already displayed it will remove it from the datagridview.. heres the problem, the program works fine until it removes a row. the next time I input the cardnum I get the error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
heres the code
private void CheckKeys3(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
string card = textBox1.Text.ToString();
int cardnum = int.Parse(card);
park = park - 1;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=car.mdb");
con.Open();
OleDbCommand queryString = new OleDbCommand("SELECT cardnum, ownerf, ownerl, make, model, year, color, chassisnumber, platenumber, controlnumber FROM vehicle WHERE vehicle.cardnum = " + cardnum + "",con);
OleDbDataReader dr = queryString.ExecuteReader();
if (dr.Read())
{
DataGridViewRow row = new DataGridViewRow();
this.dataGridView1.Rows.Add(row);
dataGridView1.Rows[i].Cells[0].Value = dr["cardnum"].ToString(); <----the error points here
dataGridView1.Rows[i].Cells[1].Value = dr["ownerf"].ToString();
dataGridView1.Rows[i].Cells[2].Value = dr["ownerl"].ToString();
dataGridView1.Rows[i].Cells[3].Value = dr["make"].ToString();
dataGridView1.Rows[i].Cells[4].Value = dr["model"].ToString();
dataGridView1.Rows[i].Cells[5].Value = dr["year"].ToString();
dataGridView1.Rows[i].Cells[6].Value = dr["color"].ToString();
dataGridView1.Rows[i].Cells[7].Value = dr["chassisnumber"].ToString();
dataGridView1.Rows[i].Cells[8].Value = dr["platenumber"].ToString();
dataGridView1.Rows[i].Cells[9].Value = dr["controlnumber"].ToString();
//this is where I remove the row
int k=i;
do
{
k = k - 1;
if (k >= 0)
{
string wah = dataGridView1[0, k].Value.ToString();
int weh = int.Parse(wah);
if (cardnum == weh)
{
dataGridView1.Rows.RemoveAt(k);
i = i - 1;
dataGridView1.Rows.RemoveAt(i);
i++;
}
}
} while (k >=0);
i++;
}
con.Close();
}
does anybody know how to fix it? or whats wrong?
Loading
theLizardPosted Mar 6, 2010, 3:39 PM
wsdfgPosted Mar 6, 2010, 5:00 AM
the reason why I said that the error was pointing here dataGridView1.Rows[i].Cells[0].Value = dr["cardnum"].ToString(); was because that was literally where c# was pointing the error when it happens. c# was right but only later did I realize why exactly was that error happening.
it happens when I delete a row then try to display or delete another 1.. so I ran the program in my head. trying to imagine how the computer would run it. then thats when I found out the error lied with the variable i.
I'll try my best explaining.
originally i=0, when you add a row the row index for that row will be equal to i which is 0, then i will increment and so on..
when you remove a row i still increments. so next time you add a row i will be higher than the row index datagridview is expecting and boom an error.
sorry for the confusion. I was confused with it myself too. and I understand I should learn to format/indent my code properly because it leads to more confusion hehe thanks for all the help anyway...
theLizardPosted Mar 4, 2010, 3:13 PM
I am sure that this threw a few people off giving you the right answer the first time round.
Do not do loops inside if or for statements if they are not required to be done there.
You pointed to this dataGridView1.Rows[i].Cells[0].Value = dr["cardnum"].ToString(); as the place of the error and the only reason you would get an error of the type you stated is that the cell was null since you created a row so the answer I first gave you was correct based on the information you gave.
When asking for help for this kind of problem you do need to be specific on where the error is actually happening.
wsdfgPosted Mar 4, 2010, 7:43 AM
int k=i;
do
{
k = k - 1;
if (k >= 0)
{
string wah = dataGridView1[0, k].Value.ToString();
int weh = int.Parse(wah);
if (cardnum == weh)
{
dataGridView1.Rows.RemoveAt(k);
i = i - 1;
dataGridView1.Rows.RemoveAt(i);
i = i - 1; <-------------------------------this is what fixed it
}
}
} while (k >=0);
I know what was wrong and what was the point of adding that line. but I cant explain. I'm a terrible teacher. thanks for all the help
Amit ChoudharyPosted Mar 4, 2010, 12:08 AM
In your code you have write :
==========================================
DataGridViewRow row = new DataGridViewRow();
this.dataGridView1.Rows.Add(row);
dataGridView1.Rows[i].Cells[0].Value = dr["cardnum"].ToString();
dataGridView1.Rows[i].Cells[1].Value = dr["ownerf"].ToString();
===========================================
you can not add row in a grid directly until and unless there's column exists in grid.
so either you bind the grid with the schema or with blank table and then add row
Or
you have to add column first and then you can add rows to the correponding column.
Please Don't forget to mark as answer if it helps.
wsdfgPosted Mar 3, 2010, 10:49 PM
theLizardPosted Mar 3, 2010, 5:37 PM
wsdfgPosted Mar 3, 2010, 5:19 PM
theLizardPosted Mar 3, 2010, 4:34 PM