datagridview不允许用户删除行

时间:2012-02-28 19:53:34

标签: c# winforms 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.";
    }

3 个答案:

答案 0 :(得分:6)

我创建了一个空的winform并将网格代码放入其中。区别在于我如何设置DataGridView.EditMode。我有= EditOnEnter,但如果我设置= EditOnKeystroke.

,则删除效果很好

答案 1 :(得分:1)

以下是我使用部分代码执行的快速示例,减去了两列。我无法复制你的问题。您可能想验证您实际上是在选择一个值,单击下拉列表中的值,然后单击“删除”。 (在这种情况下,我只是将DataGridView从工具箱拖到表单中。我没有做任何其他修改。)

public partial class Form1 : Form
{
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           DataTable myTable = new DataTable("MyTable");
           myTable.Columns.Add("CategoryID", typeof(int));
           myTable.Columns.Add("CategoryName", typeof(string));
           myTable.Rows.Add(new object[] { 1, "Small" });
           myTable.Rows.Add(new object[] { 2, "Medium" });
           myTable.Rows.Add(new object[] { 3, "Large" });

           DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
           cboCategory.HeaderText = "Category";
           cboCategory.DataSource = myTable;
           cboCategory.DisplayMember = "CategoryName";
           cboCategory.ValueMember = "CategoryID";
           cboCategory.DataPropertyName = "Category";
           cboCategory.Width = Convert.ToInt16(dataGridView1.Width * 0.20);
           cboCategory.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
           cboCategory.AutoComplete = true;

           dataGridView1.Columns.Add(cboCategory);

       }
}

答案 2 :(得分:0)

点击Delete不会删除行的另一个原因是,如果您将RowHeadersVisible属性设置为False,因此在最左侧的列中没有显示*。修复此问题后,@ MAW74656的解决方案对我有用。