Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

delete selected rows from a datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dick - 24 Sep 2004 17:25 GMT
I have a datagrid whose datasource is a datatable. I want the user to select
(multiple) rows in the datagrid, press a button and have the selected rows
deleted. Iterating through the rows in the datagrid and testing to see if
each is selected and then deleting the associated datarow from the datatable
is probably inefficient but nevertheless doesn't work because the selected
rows in the datagrid are reset as soon as the first record is deleted from
the datatable. Is there an obviously correct method for doing this?
Sijin Joseph - 26 Sep 2004 16:16 GMT
Well you could always copy the list of selected rows into a temp
collection and then delete all the rows in that collection.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> I have a datagrid whose datasource is a datatable. I want the user to select
> (multiple) rows in the datagrid, press a button and have the selected rows
[quoted text clipped - 3 lines]
> rows in the datagrid are reset as soon as the first record is deleted from
> the datatable. Is there an obviously correct method for doing this?
"Jeffrey Tan[MSFT]" - 27 Sep 2004 03:58 GMT
Hi Richard,

Based on my understanding, you want to delete several selected rows fromt
the datagrid at one time.

Yes, because of databinding, deleting one row in underlying datasource will
make DataGrid repopulates itself, and all the rows' row number behind the
deleting row will decrease one.

For this issue, I think we may use a ArrayList to store all the initial
selected row number, when deleting the row, each row number decrease the
already deleted row count. Sample code like this:

private void button1_Click(object sender, System.EventArgs e)
{
    ArrayList al=new ArrayList();
    CurrencyManager cm
=(CurrencyManager)dataGrid1.BindingContext[this.ds.Tables["your table"]];
    for(int i=0;i<cm.Count;i++)
    {
        if(this.dataGrid1.IsSelected(i))
        {
            al.Add(i);
        }
    }

    for(int i=0;i<al.Count;i++)
    {
        int row=(int)al[i];
        this.ds.Tables["your table"].Rows.RemoveAt(row-i);        //row number
decrease already deleted row count
    }
}
=======================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Dick - 29 Sep 2004 11:21 GMT
Thanks for your reply and yes it works just finr for me.

> Hi Richard,
>
[quoted text clipped - 42 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 30 Sep 2004 09:40 GMT
You are welcome, if you need further help, please feel free to tell me,
thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.