Click here to Skip to main content
15,905,013 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In My Project I'm Using a Unbounded DataGridView . In Which I made First Row as CheckBoxColumn With Header Check Box . I Provided My End User to select a single row or multiple row or Entire rows in DataGridView and can delete it if they need it . But the real Problem is When I Select all rows and click delete it deletes Entire rows and while i select a single row it performs the same action.

Below is my delete Button code .
C#
private void BtnDcDelete_Click(object sender, EventArgs e)
    {
        do
        {
            foreach (DataGridViewRow row in dataGridViewEx1.Rows)
            {
                try
                {
                    dataGridViewEx1.Rows.Remove(row);
                }
                catch (Exception) { }
            }
        } while (dataGridViewEx1.Rows.Count > 1);
    }


[edit]Spurious big and bold removed, Code block added - OriginalGriff[/edit]
Posted
Updated 9-Feb-13 21:04pm
v2
Comments
__BrokenArrow__ 10-Feb-13 3:03am    
Hi KalaiPondy here is a solution you might be able to try Let me know if you it worked for you

private void BtnDcDelete_Click(object sender, EventArgs e)
{
int i = dataGridViewEx1.Rows.Count - 1;
try {
while (!(i <= -1)) {
foreach (DataGridViewRow row in dataGridViewEx1.Rows) {
dataGridViewEx1.Rows.Remove(row);
}
}

} catch (Exception generatedExceptionName) {
}
}
OriginalGriff 10-Feb-13 3:53am    
You do realize that if it ever gets into your while loop, it will never get back out again?

1 solution

If you want to delete just the selected rows, then you need to use the SelectedRows property:
C#
foreach (DataGridViewRow row in myDataGridView.SelectedRows)
    {
    myDataGridView.Rows.Remove(row);
    }
 
Share this answer
 
Comments
__BrokenArrow__ 10-Feb-13 3:11am    
I think KalaiPond wants to remove all of the datagridview rows
KalaiPondy 10-Feb-13 8:30am    
no I need both selected rows and entire rows in datagirdview
OriginalGriff 10-Feb-13 9:27am    
Who was that directed at?
__BrokenArrow__ 24-Feb-13 6:41am    
Well its both there from me and OriginalGriff

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