469,356 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

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 22170
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.