Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datagridview not allowing user to delete row

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.

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.";
    }
like image 826
MAW74656 Avatar asked Feb 28 '12 19:02

MAW74656


People also ask

How to delete an item in a DataGridView?

Right click and delete row from datagridview After retrieveing the row index, next step is to delete the specified row. In the contextMenuStrip1_Click event delete the row using rowindex value.

How to delete selected Row in GridView using c#?

You have to specify a select button inside gridview to select a row. then you can delete a row.

How to remove Row from DataGridView in c# Windows application?

When the Delete Button is clicked, the DataGridView CellContentClick event handler is executed. If the ColumnIndex is 3 i.e. the Delete Button is clicked, then a Confirmation MessageBox us show and if the User clicks Yes button the Row will be deleted (removed) from DataGridView and Database Table.

How do I delete a Row in Winforms?

To delete rows, simply click the Delete button.


1 Answers

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.

like image 89
MAW74656 Avatar answered Sep 16 '22 20:09

MAW74656