• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4221
  • Last Modified:

Remove a row from C# datagrid

Hi all, coding c# in vs.net 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.
0
alexatsearidge
Asked:
alexatsearidge
1 Solution
 
sabeeshCommented:
You have to reload the grid
0
 
alexatsearidgeAuthor Commented:
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
0
 
riyazthadCommented:
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);.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Gautham JanardhanCommented:
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
DataTable.DefaultView.RowFilter.
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
0
 
anyoneisCommented:
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);
                  dataGrid1.Refresh();
            }
0
 
anyoneisCommented:
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.

David
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now