如何阻止datagridview复选框列复制

时间:2015-01-22 10:35:58

标签: c# checkbox datagridview

我从代码中向datagridview添加了一个复选框列,但每次复选框都会多次检查,它会不断创建更多复选框列。我该如何阻止它这样做。下面是添加列以及检查和取消选中它的代码,onload()是第一个带复选框的数据网格视图,选中复选框会加载第二个datagridview onfeatureload()。问题开始的地方,每次onfeatureload()通过点击复选框加载超过两倍的checkkbox列不断加起来

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

1 个答案:

答案 0 :(得分:0)

最好在添加之前为列添加名称,这样您就可以检查它在DGV中的存在。

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

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