Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Uncommitted New Rows Of DGV

I have unbound DGV and I wants to remove unwanted last row of it on DGV Leave EventHandller. How to do it?.

I know to add new rows to dgv by programmatically and setting the property AllowUserToAddRows = false.

But my question is : is it possible to remove last row of DGV without setting property AllowUserToAddRows = false?.

OR

Is it possible to remove uncommitted new rows of DGV?

like image 292
mahesh Avatar asked Mar 16 '11 14:03

mahesh


3 Answers

Set the DataGridView AllowUserToAddRows property to False.

However you'll have to provide a method which will allow the user to enter a new row. For example you can have that when the user double click the DataGridView, you set AllowUserToAddRows to true. And then when they are done editing, you set the value back to False.

To Add a new Row:

Lets say your DataGridView is called MyDataGridView and you have a Button called BtnAddRow and when the button is clicked, it adds an new row to your DataGridView.

private void btnAddRow_Click(object sender, RoutedEventArgs e)
{
       // Add an empty row
       MyDataGridView.Rows.Add();
}

Alternatively, you could just handle DataGridView OnDoubleClick event in which you can call MyDataGridView.Rows.Add() to add a new row.

like image 128
Jean-Louis Mbaka Avatar answered Nov 08 '22 01:11

Jean-Louis Mbaka


You can remove uncommitted new rows in datagrid view by setting AllowUserToAddRows = false

grid.AllowUserToAddRows = false;

like image 3
Sunil Kumar Avatar answered Nov 08 '22 02:11

Sunil Kumar


If your DataGridView is bound to a DataSet, this does the trick for deleting the current row:

If DataGridView1.CurrentRow.IsNewRow Then
    MyDataSet1.MyTable.Rows(DataGridView1.CurrentRow.Index).RejectChanges()
Else
    DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
End If
like image 2
Leigh Avatar answered Nov 08 '22 00:11

Leigh