Filter a datagrid to a value returned from a bindingNavigator bound to another datasource

StackOverflow https://stackoverflow.com/questions/23571692

  •  19-07-2023
  •  | 
  •  

Question

I have a form with a DataSource bound to a bindingNavigator. This DataSet includes a unique id which I want to use (when navigating to another record) to filter a second dataset and populate a datagrid with.

I am using an mdf file db with a connection set up in the database explorer window. Then in the data sources window I have 2 DataSets, 1 that produces the unique id and one that needs filtering on that id.

How do I filter the second DataSet (and the bound DataGrid) on a column in the first one?

EDIT: There isn't much code as the binding is done through the GUI. Here is the form load event

    private void frmS26_Load(object sender, EventArgs e)
    {
        this.accountMonthsTableAdapter.Fill(this.dsAccountMonths.accountMonths);
    }

This DataSet has the uniqueId in it.

EDIT 2: If I explain what I am after it may help. I have a table of transactions with each record containing a field called month_id. Month_id is a unqiue primary key (named id) in another table (accountMonths). The bindingNavigator is set to the accountMonths DataSet (each record is a calendar month). As a month is selected in the navigator, the datagrid should be filtered on the month_id field according to the id of the navigator. Hope this make sense.

Was it helpful?

Solution

You can use the BindingSource_CurrentChanged event method (which gets triggered when you navigate using the bindingNavigator), in there you can use the current object of the binding source and get its ID, based on that ID you can filter your other datasource.

private void xBindingSource_CurrentChanged(object sender, EventArgs e)
    {
            DataRow dr = (xBindingSource.Current as DataRowView).Row;
            int id=dr["id"]
            FilterOtherDataSource(id); //? 
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top