c# – datagridview不允许用户删除行

我有一个绑定到数据表的datagridview.列不是自动生成的,我在代码中创建它们.我希望我的用户能够添加和删除一行,所以我有AllowUserToAddRows = true,AllowUserToDeleteRows = true.

问题是:当他们选择一行并按DEL时,信息将被行单元格的默认值替换.这一行不会消失.

这是什么行为(为什么/发生了什么),我该如何解决这个问题?

这是我的网格创建代码:

    private void resetDowntimeGrid()
    {
        downtimeEntries = new DataTable();

        downtimeEntries.Columns.Add("Category", typeof(int));
        downtimeEntries.Columns.Add("HoursDown");
        downtimeEntries.Columns.Add("Reason");

        //deptDowntimeCategories.Select("", "ListOrder ASC");

        gridDowntime.Columns.Clear();

        DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
        {
            cboCategory.HeaderText = "Category";
            cboCategory.DataSource = deptDowntimeCategories;
            cboCategory.DisplayMember = "Name";
            cboCategory.ValueMember = "CategoryID";
            cboCategory.DataPropertyName = "Category";
            cboCategory.Width = Convert.ToInt16(gridDowntime.Width * 0.20);
            cboCategory.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            cboCategory.AutoComplete = true;

            gridDowntime.Columns.Add(cboCategory);
        }

        DataGridViewTextBoxColumn txtDowntime = new DataGridViewTextBoxColumn();
        txtDowntime.HeaderText = "HoursDown";
        txtDowntime.DataPropertyName = "HoursDown";
        txtDowntime.Width = Convert.ToInt16(gridDowntime.Width * 0.15);
        gridDowntime.Columns.Add(txtDowntime);

        DataGridViewTextBoxColumn txtReason = new DataGridViewTextBoxColumn();
        txtReason.HeaderText = "Reason";
        txtReason.DataPropertyName = "Reason";
        txtReason.Width = Convert.ToInt16(gridDowntime.Width * 0.60);
        gridDowntime.Columns.Add(txtReason);

        gridDowntime.DataSource = downtimeEntries;

        lblStatus.Text = "Downtime grid reset.";
    }

解决方法:

我创建了一个空的winform并将网格代码放入其中.不同之处在于我如何设置DataGridView.EditMode.我有它= EditOnEnter,但如果我设置它= EditOnKeystroke,删除工作完美.

上一篇:蜂鸣器选型与设计(电磁蜂鸣器篇)


下一篇:汽车“缺芯”,不要小看MCU