A
A
Alexander Sinelnikov2020-02-21 14:24:00
C++ / C#
Alexander Sinelnikov, 2020-02-21 14:24:00

How to prevent deleting empty row in dataGridView?

Good afternoon, please help, the problem is that when I delete an empty row in the datagrid, I get an error in
dataGridView1.Rows.RemoveAt (e.RowIndex)
As I understand it, you need to check the row, if it is full, then deletion is possible if it is empty, then no.

Please tell me how to implement it...

5e4fbdc4cc488604514600.png

5e4fc4be3afdc431708763.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Sinelnikov, 2020-02-21
@Straj_ua

Decided so

var senderGrid = (DataGridView)sender;
            if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
                return;

            if (e.ColumnIndex == dataGridView1.Columns["DeleteButtonColumn"].Index)
            {
                dataGridView1.Rows.RemoveAt(e.RowIndex);
            }

K
Konstantin, 2020-02-21
@nicebmw9

1. Either set AllowUserToDeleteRows = false
2. Or check the index before deleting a row

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question