lundi 17 février 2020

Insert multiple data via datagridView in c# with the use of a checkbox

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;

            }
        }

    }



Aucun commentaire:

Enregistrer un commentaire