Remove a row from C# datagrid

Posted on 2007-07-30
Last Modified: 2013-12-17
Hi all, coding c# in 2003.

I have the return dataset of a stored procedure bound to a datagrid with one of the fields being status.  The displayed records are filtered using a combo box so that all the records of one status are displayed.  The user has the ability to change the status of a single record and I'm wondering how I can update the datagrid to reflect this change.  Is there a clean way of removing a single record?

For example, if the filter is set to "Good" status then all records displayed have a status of "Good".  If the user changed the status of a record to "Bad" I'd like to remove that record since it no longer matches the filter.  I know that I could just call my stored procedure again and rebind the datagrid but I don't want my grid to be refreshed.  Let's say the user has scrolled down three pages and changes the status of a record, a refresh would repopulate and lose their selection/sorting.
Question by:alexatsearidge
    LVL 8

    Expert Comment

    You have to reload the grid

    Author Comment

    My client is particular about this.  He doesn't want the grid to be refreshing and jumping all over.  Is there a seamless way to rebind and minimalize load times
    LVL 6

    Accepted Solution

    Better to use DataView as data source for grid. And filter as per user selected by using Filter method of dataview. You can also delete a row by using dataGridView1.Rows.RemoveAt(1);.

    LVL 29

    Expert Comment

    by:Gautham Janardhan
    or @ first u could load all the data from the back end
    and each time a filter is applied instead of fetching from the back end  set the
    but this is advisable only when the total number of records is comparitively low
    else the loaading the first time would take a long time
    LVL 11

    Expert Comment

    This should work just fine:

    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
                      string status = comboBox1.SelectedItem.ToString();
                      dataSet1.Tables[0].DefaultView.RowFilter = string.Format("Status = '{0}'", status);
    LVL 11

    Expert Comment

    The only other code of note is:

         dataGrid1.DataSource = dataSet1.Tables[0];

    When you bind to a table, you are really binding to a view - specifically the default view for the table. This default view has a RowFilter which you can set dynamically. The DataGrid, responds to any changes in the datasource, including those induced by changing data in a row (so that the filter excludes them) and by changes in the filter.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now