datagridview 不允许用户删除行 [英] datagridview not allowing user to delete row

查看:22
本文介绍了datagridview 不允许用户删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a datagridview bound to a datatable. The columns are not autogenerated, I create them in code. I want my user's to be able to add and delete a row, so I have AllowUserToAddRows = true, AllowUserToDeleteRows = true.

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

The problem is: when they select a row and press DEL, the information is replaced by the row cell's default values. The row does not go away.

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

What is this behavior (why/what is happening), and how can I get around it?

这是我的网格创建代码:

Here is my grid creation code:

    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.

I created an empty winform and just put the grid code into it. The difference was how I had the DataGridView.EditMode set. I had it = EditOnEnter, but the delete works perfectly if I set it = EditOnKeystroke.

这篇关于datagridview 不允许用户删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆