0

I have this code here, i wish to insert multiple students marks to the database through the datagridview once a checkbox is checked. The problem with the code below is that only the data from the first checked row is inserted. What can i do to ensure every selected row is inserted.

    public override void Savebtn_Click(object sender, EventArgs e)
    {
        int select;
        for (int i = 0; i <= dataGridView1.Rows.Count -1; i++)
        {
            DataGridViewRow row = dataGridView1.Rows[i];
            if((bool)row.Cells["CheckBoxColumn"].FormattedValue == true)
            {
                select++;
            }

        }
        if(select == 0)
        {
            MainClass.showMSG("Please Select at least one Student", "Error", "Error");
        }
        for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
        {
            DataGridViewRow row = dataGridView1.Rows[i];

            if ((bool)row.Cells["CheckBoxColumn"].FormattedValue == true)
            {
              obj.st_InsertResults(Convert.ToInt32(row.Cells["stdIDGV"].Value.ToString()), 
               row.Cells["StdNameGV"].Value.ToString(),
               Convert.ToInt32(row.Cells["EngGV"].Value.ToString()));
                MainClass.showMSG(" Data added successfully.", "Success....", "Success");
                MainClass.disable_reset(panel1);
                dataGridView1.DataSource = null;

            }
        }

    }

1 Answer 1

1

Why are you setting your datasource to null?

dataGridView1.DataSource = null;

try removing this line, should work!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ,that worked althogh it creates a new problem. How do i referesh the rows after inserting? because i intially used the null datasource to do so.
I believe the objective is to iterate through all rows and do work on it. Why do you need to refresh the rows? the loop should complete itself and add rows.
you can move last three lines outside the loop (set a bool in there) and show message after the work is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.