Windows Develop Bookmark and Share   
 index > Windows Forms General > Delete a new row with code in a DataGridview
 

Delete a new row with code in a DataGridview

How can I delete a newly added row with code in a DataGridview that has not been flushed to the datasource?Suppose a user starts a new row at the bottom of the grid but does not fill in all required information and then clicks in a row above the new row. That user just wants to forget the insert.

The DataGridView is bound to a binding source which is in turn bound to a datatable. There is no delete method in the binding source and the record does not exist in the datatable because it is new and has not been flushed to the BindingSource.Datasource. I thought theDataSource.RemoveCurrent method would work but it removes the row above the new row. Pressing escape removes the newly added row and it seems that the method that Escape calls should be available for calling. Is it?

Price Brattin, SQLServer MCP, Microsoft Dynamics SL Consultant
Price Brattin  Monday, January 12, 2009 8:22 PM
Hi Price -

I doubt you're still struggling over this, but since you said you wanted to duplicate what happens when the user presses the 'Escape' key, could you use the admittedly-hackish:

SendKeys.Send(Microsoft.VisualBasic.Chr(Windows.Forms.Keys.Escape))

??

I'm having issues with this when the user has done a sort using a column header, with the "new row" line ending up somehow no longer at the bottom (my solution, for now, is disabling column-header sorting, lol).

Kirk

Kirkaiya  Thursday, October 08, 2009 7:09 AM
Hi,

Can you please try the following piece of code?

foreach
(DataGridViewRow row in this.dataGridView1.SelectedRows)

{

if (row.Index != this.dataGridView1.Rows.Count)

{

this.dataGridView1.Rows.Remove(row2);

}

}

Thanks,
Karthizen

Karthizen  Monday, January 12, 2009 9:21 PM
Thanks for your reply.

I tried your code and there are no selected rows in this situation. The user clicks in the addNew row, realizes it is a mistake, and clicks back into a good row. No row is selected.

I tried also the following:

dgv1.Rows.Remove(dgv1.CurrentRow)

But it throws the following error:Uncommitted new row cannot be deleted.

Any other ideas?



Price Brattin, SQLServer MCP, Microsoft Dynamics SL Consultant
Price Brattin  Monday, January 12, 2009 9:35 PM

Hi Price Brattin,

Do you mean you want to remove the last row if the last row is incomplete? Like @Karthizen says, we can remove the selected rows through programming.

> Uncommitted new row cannot be deleted.

Note that the row for new records, whichwith an asterisk in the rowheader, cann’t be removed. If we enter the cell ofnew row, and don'ttype any character, then we delete the currentnew row,it will throw the error.

Whenwe edit a cell and set the focus to another cell indifferent row (expect the now row), it trigger validation of old cell and old row and new row enter event.We'd better to comfirm the user todelete theincomplete row. Just for your convenience, I wrote the following code.

DataTable dt1;

private void Form1_Load(object sender, EventArgs e)

new DataTable("dt1");

"id");

"name");

"tag");

"a", "aaa", "a01");

"b", "bbb", "b02");

this.dataGridView1.RowEnter += new DataGridViewCellEventHandler(dataGridView1_RowEnter);

this.dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);

bool flag = false;

void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)

true;

void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)

if (flag == true & e.RowIndex < dataGridView1.NewRowIndex)

false;

int lastindex = dataGridView1.NewRowIndex - 1;

if (dataGridView1.Rows[lastindex].Cells[0].Value == null || dataGridView1.Rows[lastindex].Cells[1].Value.ToString() == "")

DialogResult res = MessageBox.Show(" " + (lastindex +1).ToString() + " is incomplete.\n\nDo you want to delete it?", "Note", MessageBoxButtons.YesNo);

if (res == DialogResult.Yes)

//bound

// unbound

//dataGridView1.Rows.RemoveAt(lastindex);

If I misunderstood you, or you have any other questions, please feel free to tell me.

Best regards,

Ling


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, January 18, 2009 10:47 AM
Sorry, but this is not the answer to my question but it is close. My problem is the cell validating and the row leave events fire before the rowEnter event fires. So focus does not leave the row. The stated question was this:How can I delete a newly added row with code in a DataGridview that has not been flushed to the datasource?Suppose a user starts a new row at the bottom of the grid but does not fill in all required information and then clicks in a row above the new row. That user just wants to forget the insert.

The row to delete has not been selected so the selected idea does not work. The row in question is in the row above the row with the asterisk in the selector; it is the one with the pencil and the elypsis in the selector. It can be deleted because when the user presses ESC it is deleted. How can we duplicate in code what happens when the user presses ESC? If the user clicks on another row, I want to throw up a msgbox that asks, "Do you wish to abandon this row". If I put this code in the rowleave event, the various cell validating events fire and cause problems. Focus does not leave the row or the cell, depending on what the user was doing at the time.But when the user presses ESC the cell validating events do not fire and focus changes to another row. Surely there is a way in code to do what happens when the user presses ESC.

Also, I would like the msgbox to fire even when the user presses ESC. Can this be done?

Price Brattin, SQLServer MCP, Microsoft Dynamics SL Consultant
Price Brattin  Thursday, January 22, 2009 8:05 PM
Hi, Price, did you solve your problem?

Are you using bindingSource?

I faced the same problem. I put a cancel button in the toolstrip with the following code:

MyBindingSource.RemoveCurrent();

see if works.

Makoto

Roberto Makoto  Thursday, February 19, 2009 5:46 PM
I also would like to know the answer to this one. I have a datagridview(dgv) bound to a SQL table. I'm using a table adapter and when I enter the dgv, default needed values are added. When I edit one of the cells, it adds a new row which is to be expected. I found the following by hitting the down cursor key. It adds a new row with the default values and when I cursored back up, thenew line "appeared to go away" but in reality was written back out to the database table when I eventually saved it. I tested a bit further and found that if I did the cursor down/cursor up several times, I had multiple blank lines also written out to the database.

Now I know if you press the Escape key, it deletes the "new" unwanted record so there has to be a way of getting rid of those unwanted records by some form of code (preferably without having to intercept keystrokes and mess with the normal functionality). Any else have some input?

J F Sohm
If at first you don't succeed, don't try sky diving !!!
JF Sohm  Tuesday, September 15, 2009 3:47 PM
Hi Price -

I doubt you're still struggling over this, but since you said you wanted to duplicate what happens when the user presses the 'Escape' key, could you use the admittedly-hackish:

SendKeys.Send(Microsoft.VisualBasic.Chr(Windows.Forms.Keys.Escape))

??

I'm having issues with this when the user has done a sort using a column header, with the "new row" line ending up somehow no longer at the bottom (my solution, for now, is disabling column-header sorting, lol).

Kirk

Kirkaiya  Thursday, October 08, 2009 7:09 AM

You can use google to search for other answers

Custom Search

More Threads

• how can use a dynamic diagram to show the data transfer?
• making Que threadsafe
• Maximized State Problem
• Delete - menu / textbox
• Working with multiple forms
• Icons in menus
• Sub Main declared twice?
• Forcing a component to use default colors
• mails are not going out side of my domain
• ScrollableControl & wheel tilt?