ArgumentOutOfRangeException in Datagridview

dualshck012

Active member
Joined
Jan 14, 2018
Messages
29
Location
Leyte, Philippines
Programming Experience
1-3
Why i'm getting this problem? 'ArgumentOutOfRangeException' was unhandled' in line #5.
- 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.

C#:
private void Sett_icts_DGV_prodAssign_CellValueChanged(object sender, DataGridViewCellEventArgs e)
string prodid;
int prodid1;
        {
            prodid = Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString();

            if (prodid == "")
            {

                prodid1 = 0;
            }
            else
            {
                prodid1 = Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString());
            }

            if (prodid1 == 0)

            {
                conn2.Open();
                string query601A = "INSERT INTO ihsasprodassign(prodassigndesc, prodassignkey, prodassigndte, prodassigntme, prodassignstat) VALUES('" + Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassigndesc"].Value.ToString() + "' , '" + Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignkey"].Value.ToString()) + "' , '" + DateTime.Now + "','" + DateTime.Now + "', 'A')";
                cmda = conn2.CreateCommand();
                cmda.CommandType = CommandType.Text;
                cmda.CommandText = query601A;
                cmda.ExecuteNonQuery();
                conn2.Close();

            }
            else
            {
                conn2.Open();
                string query601B = "UPDATE ihsasprodassign set prodassigndesc='" + Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassigndesc"].Value.ToString() + "', prodassignkey='" + Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignkey"].Value.ToString()) + "',prodassignmod='" + DateTime.Now + "' WHERE prodassignid=" + mysqlconstring.prodid1 + "";
                cmda = conn2.CreateCommand();
                cmda.CommandType = CommandType.Text;
                cmda.CommandText = query601B;
                cmda.ExecuteNonQuery();
                conn2.Close();      

            }







        }
 
There are obviously two possibilities there so you need to work out which is the problem. You should have already done that. You can use the debugger to evaluate the relevant values or you can change this:
C#:
prodid = Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString();
into two lines:
C#:
var temp = Sett_icts_DGV_prodAssign.Rows[e.RowIndex];

prodid = temp.Cells["prodassignid"].Value.ToString();
Now you'll be able to tell whether it's e.RowIndex or "prodassignid" that is out of range. It rather helps to know what problem you're trying to solve first.

If it's the former then have a look at the value and it should be fairly obvious when the exception is thrown what the valid range is and why that's not in it. If it's the latter then you don't have a column in the grid with that name. Note that the header text is NOT the name, even if they have the same value.
 
I just reread your post and noticed the bit about the value being non-negative, so the issue is almost certainly the row index. As I said, actually use the debugger and look at the value and the issue should be obvious.
 
BTW, this is terrible:
C#:
string prodid;
int prodid1;
Anyone without prior knowledge wouldn't know what those variables were for. If the ID is a number then the numeric variable is the one that should be named prodid. The logical name for the string variable would be something like prodidtext.
 
There are obviously two possibilities there so you need to work out which is the problem. You should have already done that. You can use the debugger to evaluate the relevant values or you can change this:
C#:
prodid = Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString();
into two lines:
C#:
var temp = Sett_icts_DGV_prodAssign.Rows[e.RowIndex];

prodid = temp.Cells["prodassignid"].Value.ToString();
Now you'll be able to tell whether it's e.RowIndex or "prodassignid" that is out of range. It rather helps to know what problem you're trying to solve first.

If it's the former then have a look at the value and it should be fairly obvious when the exception is thrown what the valid range is and why that's not in it. If it's the latter then you don't have a column in the grid with that name. Note that the header text is NOT the name, even if they have the same value.

I already checked both of the two possibilities, but still ended on the same error. Maybe the problem is the configuration of the datagridview itself since i'm using bound columns here. All the columns are present.
1631016620922.png
 
It's like you didn't even look at your own screenshot. What value are you using to index the Cells property? Look at the screenshot you posted. Is that value the name of the column?
 
Back
Top Bottom