472,111 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

how to programatically delete mutiple selected rows in a datagridview?


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
4 22279
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
Yes, I have both refresh and acceptchanges. No help. Thanks.

Dec 4 '05 #3
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
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.

Similar topics

5 posts views Thread by Mojtaba Faridzad | last post: by
reply views Thread by Diego | last post: by
1 post views Thread by Mark | last post: by
1 post views Thread by Tim Kelley | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.