By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

how to programatically delete mutiple selected rows in a datagridview?

P: n/a

Can somebody tell me how to delete multiple rows in a datagridview
selected by the user?

I tried the following two methods to no avail. I have a myDataGridView
that is bound to
myDataTable's myDataView. The column "Name" is the primary key.
I want to delete the rows selected by the user:

foreach (DataGridViewRow drv in myDataGridView.SelectedRows)
{
// this is not working, no error but does nothing
foreach (DataRowView drview in myDataView)
{
if (drview["Name"].Equals(drv.Cells["Name"]))
{
drview.Delete();
}
}

// nor is this working, same effect
foreach (DataRow dr in myDataTable.Rows)
{
if (dr["Name"].Equals(drv.Cells["Name"]))
{
dr.Delete();
}
}
}

I've searched through all the info I could, still could find anything.
Any help is
much appreciated!

Henry Jiang

Dec 4 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Did you try to delete rows from data table and refresh grid?
"Henry J." <ta********@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

Can somebody tell me how to delete multiple rows in a datagridview
selected by the user?

I tried the following two methods to no avail. I have a myDataGridView
that is bound to
myDataTable's myDataView. The column "Name" is the primary key.
I want to delete the rows selected by the user:

foreach (DataGridViewRow drv in myDataGridView.SelectedRows)
{
// this is not working, no error but does nothing
foreach (DataRowView drview in myDataView)
{
if (drview["Name"].Equals(drv.Cells["Name"]))
{
drview.Delete();
}
}

// nor is this working, same effect
foreach (DataRow dr in myDataTable.Rows)
{
if (dr["Name"].Equals(drv.Cells["Name"]))
{
dr.Delete();
}
}
}

I've searched through all the info I could, still could find anything.
Any help is
much appreciated!

Henry Jiang

Dec 4 '05 #2

P: n/a
Yes, I have both refresh and acceptchanges. No help. Thanks.

Dec 4 '05 #3

P: n/a
I managed to delete some rows using:

myDataView.Sort = "Name";
int idx = myDataView.Find(drv.Cells["Name"].Value);
myDataView.Delete(idx);

However, the problem is that it is not always the selected rows that
are deleted,
especially if I sort the datagrid before I delete. In other words,
after myDataView
is sorted by "Name", the ordering is no longer what is shown in the
datagrid.
Some other rows may get deleted.

Now the question remains, how to link the selected rows in the UI to
the rows in
the DataView? DataView.Find can only find the rows if the DataView is
sorted...

Dec 4 '05 #4

P: n/a
solved by:

foreach (DataGridViewRow drv in
myDataGridView.SelectedRows)
{
myDataGridView.Rows.Remove(drv);
}

My oversight. Didn't look at .Rows.Remove().

Dec 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.