Filtering Dataset

After filling a dataset I would like to further filter the results without repopulating the dataset.  Any help would be greatly appreciated.


Answer this question

Filtering Dataset

  • Gerd71

    One way of doing this.

    First I would bind the listbox by doing the following (which I assume you are doing):

    listBox1.DataSource = myView;
    listBox1.DisplayMember = "ColumnToDisplay";
    listBox1.ValueMember = "PIDColumn";

    When it comes time to get the selected value or PID use the SelectedValue property.

    string PID = (string)listBox1.SelectedValue;

    You can get to the underlying table by using the Table property of the DataView.

    DataView view = (DataView)listBox1.DataSource;
    DataTable table = view.Table;

    Now you can do whatever you want with the table and its rows.  Any changes to the table data is reflected in the view as well.

    I feel that last part about the table is kind of dirty but it works.  Someone else may chime in here with a cleaner answer.  I'm still playing with it and if I find anything better I'll post it.


  • Steve Wang 2006

    Hi, and thanks for responding.  I'm having some success using a DataView which I've called using the following code:

    private System.Data.DataView dvPatientLookup;

    dvPatientLookup = this.dsPatientLookup1.Tables["tblPatientIndex"].DefaultView;

    dvPatientLookup.RowFilter = "patientFullName LIKE '%"+ textBox1.Text.Trim() +"%'";

    I've bound a listbox to the dataview and I get the correct results but I don't know how to access elements of the dataview


  • pedromc

    DataViews are just simply views into the underlying table.  Viewing and changing data in DataView is also changed in the underlying table.  Where you might use DataRow to retreive a row from the Rows collection of a DataTable you use the DataRowView to get to the rows in a DataView.

    From the MSDN:

    DataTable custTable = custDS.Tables["Customers"];
    DataView custView = custTable.DefaultView;
    custView.Sort = "CompanyName";

    custView.AllowDelete = false;

    DataRowView newDRV = custView.AddNew();
    newDRV["CustomerID"] = "ABCDE";
    newDRV["CompanyName"] = "ABC Products";
    newDRV.EndEdit();

    You can also use the foreach statement on the DataView
        foreach (DataRowView view in DataView)
        {
            view["MyColumn"] = "New Value";
        }

    From what I see you can use the DataView just as you might use a DataTable with some slight differences.

    Is this closer to what you are looking for



  • Ian Bell

    See if the DataTable.Select method works for you.

  • Storm Devil

    Thanks so very much for all your help.  It's the part that I was missing.  Thanks again.


  • ApellA

    Rather use a Dataview.

    Get the View from the Dataset and then do your filtering on the Dataview.

  • bmyersbook

    I'm using the following code to pull the table index value:

    PID = this.myDataSet.Tables["myTable"].Rows[this.listBox1.SelectedIndex]["myIndex"]); 

    How do I declare 'view' without using the foreach statement so I can pull index value from the 'view' like I did with the dataset



  • Filtering Dataset