如何防止 datagridview 复选框列复制
how do I prevent datagridview checkbox column replication
我从代码中向 datagridview 添加了一个复选框列,但每次复选框被多次选中时,它会不断创建更多复选框列。我如何防止它这样做。下面是添加列以及选中和取消选中它的代码,onload() 是第一个带有复选框的 datagridview,选中一个复选框加载第二个 datagridview onfeatureload()。这就是问题开始的地方,每次通过单击复选框两次以上来加载 onfeatureload() 时,复选框列不断加起来
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);
}
这里是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);
}
}
这是我的表现 check/uncheck 也是
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();
}
最好在添加之前为您的列命名,这样您就可以检查它是否存在于 DGV 中。
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.Name = "myColumn";
// Set any other desired properties ...
if (!dataGridView1.Columns.Contains(col.Name))
{
dataGridView1.Columns.Add(col);
}
我从代码中向 datagridview 添加了一个复选框列,但每次复选框被多次选中时,它会不断创建更多复选框列。我如何防止它这样做。下面是添加列以及选中和取消选中它的代码,onload() 是第一个带有复选框的 datagridview,选中一个复选框加载第二个 datagridview onfeatureload()。这就是问题开始的地方,每次通过单击复选框两次以上来加载 onfeatureload() 时,复选框列不断加起来
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);
}
这里是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);
}
}
这是我的表现 check/uncheck 也是
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();
}
最好在添加之前为您的列命名,这样您就可以检查它是否存在于 DGV 中。
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.Name = "myColumn";
// Set any other desired properties ...
if (!dataGridView1.Columns.Contains(col.Name))
{
dataGridView1.Columns.Add(col);
}