【问题标题】:duplicate Combobox is adding in cells in datagridview重复的组合框在 datagridview 的单元格中添加
【发布时间】:2015-06-15 08:38:38
【问题描述】:

我在 datatgridview 中使用动态组合框并正确填充。 但是,当我尝试为 datagridview 添加新行时,通过单击按钮 Add New Row,此组合框在单元格中重复出现。 如果我单击一次,将在 cell1 中添加一个组合框,第二次单击以添加一行,在单元格中添加两个组合框。 如何在 datagridview 中添加新行而不在单元格中重复此组合框? 我的代码如下:

 private void btnRMAdd_Click(object sender, EventArgs e)
        {
            //add new row
            dgItemGroup.Rows.Add();
            fnInsertColumnsCombo();
        }

 private void fnInsertColumnsCombo()
        {
            DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
            combo1.Name = "GroupName";
            combo1.HeaderText = "GroupName";
            string sqlStr = "SELECT GroupID,GroupName FROM tblITEMGROUP ";
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            combo1.DataSource = dt;
            combo1.DisplayMember = "GroupName";
            combo1.ValueMember = "GroupID";
            combo1.AutoComplete = true;
            combo1.FlatStyle = FlatStyle.Flat;
            dgItemGroup.Columns.Insert(0, combo1);
            dgItemGroup.Columns[0].Width = 250;
        }

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    在您的方法 fnInsertColumnsCombo 中,每次添加新行时都会向网格添加新列。每次添加新行时都会调用此方法。也许您想在“init”阶段添加一列,然后在添加新行后填充数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多