1
votes

i added a checkbox column to datagridview from code but each time checkbox is checked more than once it keeps creating more checkbox columns. how do i prevent it from doing that. below is code for adding column as well as checking and unchecking it, onload() is the first datagridview with checkboxes upon which a checkbox is checked loads up the second datagridview onfeatureload(). thats where the problem begins, each time onfeatureload() is loaded by click of a checkbox more than twice the checkkbox column keeps adding up

     private void onload()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

        using (connection = new MySqlConnection(connectionString))
            if (this.OpenConnection() == true)
            {
                MySqlCommand sql = new MySqlCommand("sp_profgridview", connection);
                sql.CommandType = CommandType.StoredProcedure;
                mySqlDataAdapter = new MySqlDataAdapter(sql);
                DataSet dp = new DataSet();
                mySqlDataAdapter.Fill(dp);
                sql.ExecuteNonQuery();
                kryptonDataGridProf.DataSource = dp.Tables[0];
                kryptonDataGridProf.Columns[0].Visible = false;
                kryptonDataGridProf.Columns[1].Width = 120;
           //     kryptonDataGridProf.Columns[2].Width = 150;

                //DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                //kryptonDataGridProf.Columns.Add(chk);
                //chk.HeaderText = "Check Data";
                //chk.Name = "chk";
                //kryptonDataGridProf.Rows[0].Cells[0].Value = true;

                    DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                    doWork.HeaderText = "CHECK PROFILE";
                    doWork.FalseValue = "0";
                    doWork.TrueValue = "1";
                    kryptonDataGridProf.Columns.Insert(3, doWork);


            }

here is onfeatureload()

  private void onfeatureload()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

        using (connection = new MySqlConnection(connectionString))
            if (this.OpenConnection() == true)
            {
                MySqlCommand sqlf = new MySqlCommand("sp_featgridview", connection);
                sqlf.CommandType = CommandType.StoredProcedure;
                mySqlDataAdapter = new MySqlDataAdapter(sqlf);
                DataSet ds = new DataSet();
                mySqlDataAdapter.Fill(ds);
                sqlf.ExecuteNonQuery();
                kryptonDataGridView2.DataSource = ds.Tables[0];
                kryptonDataGridView2.Columns[0].Visible = false;
                kryptonDataGridView2.Columns[1].Width = 100;

                DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
                DatagridViewCheckBoxHeaderCell cbH = new DatagridViewCheckBoxHeaderCell();
                colCB.HeaderText = "CHECK FEATURE";
                kryptonDataGridView2.Columns.Add(colCB);

            }
    }

and here's how i perform check/uncheck also

   private void kryptonDataGridProf_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
            ch1 = (DataGridViewCheckBoxCell)kryptonDataGridProf.Rows[kryptonDataGridProf.CurrentRow.Index].Cells[0];

            if (ch1.Value == null)
                ch1.Value = false;
            switch (ch1.Value.ToString())
            {
                case "True":
                    ch1.Value = false;
                     break;
                case "False":
                    ch1.Value = true;
                    onfeatureload();

                    break;
            }

            ch1.Value.ToString();
    }
1
you should (at least) test the existence of the checkbox column before adding it - tschmit007
the chechbox column exists but only keeps adding after 2 or more checks - RichieCr7
yes because you call onfeatureload that adds another and another... - tschmit007
precisely what i want to correct, is there a way to make it static - RichieCr7

1 Answers

0
votes

It is a good idea to give your column a name before adding, this way you can check for its existence in the DGV.

DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.Name = "myColumn";
// Set any other desired properties ...

if (!dataGridView1.Columns.Contains(col.Name))
{
    dataGridView1.Columns.Add(col);
}