【问题标题】:how do I prevent datagridview checkbox column replication如何防止datagridview复选框列复制
【发布时间】:2015-01-22 10:35:58
【问题描述】:

我从代码中向 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);

            }
    }

这也是我执行检查/取消检查的方式

   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();
    }

【问题讨论】:

  • 你应该(至少)在添加之前测试复选框列的存在
  • chechbox 列存在,但仅在 2 次或更多检查后才继续添加
  • 是的,因为您调用 onfeatureload 会添加另一个和另一个...
  • 正是我想要纠正的,有没有办法让它静态

标签: c# checkbox datagridview


【解决方案1】:

在添加之前为您的列命名是个好主意,这样您就可以检查它在 DGV 中是否存在。

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

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

【讨论】:

  • ty 但我已经解决了它,无论如何它与你的很接近,而且我还给了它一个 foreach 语句来默认检查它们。所以我会把你的解决方案打勾作为答案
  • @galaxybomb 很高兴你能弄明白!
猜你喜欢
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多