Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..

i want to remove the current row in the data grid how can i do this. and i wants to write this in cell enter event.
what i am doing here is in cell enter event in the data grid i am checking weather the the entered value is already there in the database. if it so i am clearing that entered value and wants to remove that row. here i don't know how to remove this row.

thank you...
Posted
Updated 21-Aug-17 3:16am
v3

Supposing the name of the Datagrid used is dgvGrid
Now to remove the currently selected row please use the following code

dgvGrid.Rows.RemoveAt(dgvGrid.CurrentRow.Index);

||ly you cam remove row at any index by writing the row index you want to remove

such as removing row at index 'i' code is as follows:-

dgvGrid.Rows.RemoveAt(i);

i guess this helps..
 
Share this answer
 
You can use Row validating event.

Check in database for duplication. Now if dont want user to enter that garbage value, Turn e.Cancel = True it will not allow user to go for next row.

It will not remove row but it will not allow to for next one.

private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells["name"].Value.ToString() == "test")
{
MessageBox.Show("can't enter");
e.Cancel = true;
}

}

Just test this code.
Hope it will work for u.
 
Share this answer
 
hi...

here, i wanted to write this code in cell enter event. but when i write it there its giving an error saying:
"Operation cannot be performed in this event handler."

so what i have to do now.

(i really don't know how can i reply for the answered you have given. i hope this is the way how i should do.)

thanks again..
 
Share this answer
 
even when i write the code in cell leave event its giving an same error as before.:confused:


and i do want to go to next row after removing the current row. so cannot use that e.Cancel = true;

as im so new to the c#, im so confused what to do.
thanks for all the answers.
 
Share this answer
 
v2
if (e.KeyCode == Keys.Delete) {
	int x = DataGridView1.CurrentRow.Index;

	DataGridView1.Rows.Remove(DataGridView1.Rows(x));
}
 
Share this answer
 
Comments
Kats2512 21-Aug-17 9:31am    
Well done! Afraid you are 7 years too late!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900