Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing last empty row of dataGridView

I have some empty datagridview which uses data source is List through BindingSource. Because AllowUserToAddRows = true, initially the datagridview has one empty row. When I just select this row and close the form then datagridview creates new empty object. Why it does it? How to prevent this? Thanks.

Selecting of empty row: enter image description here And I get this: enter image description here

Conclusion: After many attempts to find a solution I decided to create UI control with datagridview and two buttons (add row and delete row). At this point I stopped trying to do as planned, but I'd still be pleased to know solutions.

like image 381
Andrei Orlov Avatar asked Jan 31 '26 16:01

Andrei Orlov


1 Answers

You can handle RowValidating event to verify if row is empty. If so, then cancel event and row will not be added.

if (gridView[0, e.RowIndex].Value == null)
    e.Cancel = true;
like image 193
Sergey Berezovskiy Avatar answered Feb 02 '26 04:02

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!