adil sharif
adil sharif

Reputation: 55

DataGridView Rows Error Index Out of Range c#

I am working on C# WinForms where i have some checkboxes, whose text are added in datagridview (dgData) on a button click. Here is its code.

private void btnAdd_Click(object sender, EventArgs e)
    {
        dgData.Rows.Clear();
        foreach(Control c in pnlDeficiency.Controls)
        {
            if ((c is CheckBox) && ((CheckBox)c).Checked)
            dgData.Rows.Add(c.Text);
        }
    }

And my save code is here.

private void btnSave_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Are You Sure You Want to Save the Record!", "TAC | Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
        {
            SqlConnection S_Conn = new SqlConnection(strConnString);
            S_Conn.Open();
            int a = 10;
            for (int i = 0; i <= dgData.Rows.Count; i++)
            {
                string Query_Insert = "";
                Query_Insert = "INSERT into Deficiency_Details (Vendor_Id, Tender_Id, DeficiencyType) values ('" + cmbSelectVendor.SelectedValue + "', '" + cmbSelectTender.SelectedValue + "', '" + dgData.Rows[i].Cells[0].Value.ToString() + "')";
                SqlCommand Command_Insert = new SqlCommand(Query_Insert, S_Conn);
                a = Command_Insert.ExecuteNonQuery();
            }
            if (a == 0)
            {
                MessageBox.Show("Record Not Saved Successfully! Please Check Fields", "TAC | Alert", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Record Saved Successfully", "TAC | Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
            S_Conn.Close();
            S_Conn.Dispose();
            ResetAll();
        }
    }

The error i get is Index out of rannge. I have debugged my program and the error is in this line

dgData.Rows[i].Cells[0].Value.ToString()

Where VALUE is going NULL. I know the error and i solved it many times before but this time its not solving at all. please help me.

Upvotes: 0

Views: 1588

Answers (2)

user7145771
user7145771

Reputation: 1

Use following code where you can specify the index of the cell which you need to insert into

DataGridViewRow row = (DataGridViewRow)dgData.Rows[0].Clone();
row.Cells[0].Value = c.text;
dbData.Rows.Add(row);

Upvotes: 0

Sebi
Sebi

Reputation: 3979

The Problem is this line:

for (int i = 0; i <= dgData.Rows.Count; i++)

You need to change it to this:

for (int i = 0; i < dgData.Rows.Count; i++)

Because let's say you got 10 Rows in your GridView. dgData.Rows.Count would be 10. Now you run from 0 to 10 because of your <= Operator. 0-10 would be 11 loop runnings. Your GridView only contains 10 rows. So the Index is out of range on your last loop iteration. If you change to < Operator your loop is just running from 0-9 which are exactly 10 iterations.

Upvotes: 1

Related Questions