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();
}
Aucun commentaire:
Enregistrer un commentaire