Datagridview组合框值更改时生成空行

时间:2015-05-17 07:10:36

标签: c# winforms datagridview

我有一个包含组合框列和dateTimePicker的Datagridview, 问题是在选择了组合框值后,gridview会自动添加3个或更多空行,我不知道问题出在哪里

所以这是发生的事情: 当我从下拉列表中选择并移动到下一个单元格时出现空行,或者甚至当我在文本框中写入并移动以单击其他单元格时出现的行显示

enter image description here

Empty Rows appears Automatically

这是我如何生成datagridview的代码:

  DataGridViewComboBoxColumn dgvcombo =new DataGridViewComboBoxColumn();
                dgvcombo.ValueType = typeof(string);
                dgvcombo.Name = cntrlname;
                dgvcombo.HeaderText = Headertext;
                dgvcombo.DataPropertyName = cntrlname;
                dgvcombo.Tag = ToolTipText;
                dgvcombo.DataSource = Data.GetLookup(ComboData[0]);
                dgvcombo.ValueMember = ComboData[1];
                dgvcombo.DisplayMember = ComboData[2];
                GridViewList.Columns.Insert(ColumnIndex, dgvcombo);
private void GridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == DateColumnIndex && e.RowIndex != -1)
            {ateTimePicker = new DateTimePicker();
                    dateTimePicker.TextChanged += new EventHandler(dateTimePicker_OnTextChange);
                    GridView.Controls.Add(dateTimePicker);
                    dateTimePicker.Format = DateTimePickerFormat.Short;
                    Rectangle dgvRectangle = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                    dateTimePicker.Size = new Size(dgvRectangle.Width, dgvRectangle.Height);
                    dateTimePicker.Location = new Point(dgvRectangle.X, dgvRectangle.Y);
                    dateTimePicker.CloseUp += new EventHandler(dateTimePicker_CloseUp);


private void dateTimePicker_OnTextChange(object sender, EventArgs e)
        {
            GridView.CurrentCell.Value = dateTimePicker.Text.ToString();
            //dateTimePicker.Visible = false;
        }

datagridview绑定到sql数据表,因此是组合框。

0 个答案:

没有答案