Cocoa Dev
Cocoa Dev

Reputation: 9541

BindingSource.Filter using Inner Join

I am using BindingSource and I want to use some SQL code to perform an Inner Join. My code for this doesn't work

ticketsBindingSource.Filter = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user WHERE u.CallerName = 'joe.smith' AND t.ProblemStatus = 'Open'";

But the following does work

ticketsBindingSource.Filter = "ProblemStatus = 'Open'";

How can I run my InnerJoin query and update my datagridview?

Upvotes: 0

Views: 4093

Answers (2)

Taryn
Taryn

Reputation: 247680

The BindingSource.Filter applies a Filter to the data that is already been loaded into your DataGridView. So if you have accounts displayed, and you are looking for one specific account, then you would filter that data by account number.

The Filter, cannot run another SQL query for you.

If you want to perform an INNER JOIN on that data then you will need to perform another search and load the DataGridView. It sounds like you do not want to perform a filter, but you want to display two separate sets on data in the same grid.

Your basic process would be:

  1. Load you DataGridView
  2. Use selects the Filter or records they want
  3. Performs a second search with your INNER JOIN
  4. reload your DataGridView with the new data.

EDIT here is some code that might get your started:

How to: Bind Data to the Windows Forms DataGridView Control

private void GetData(string selectCommand)
{
    try
    {
        // Specify a connection string. Replace the given value with a 
        // valid connection string for a Northwind SQL Server sample
        // database accessible to your system.
        String connectionString =
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";

        // Create a new data adapter based on the specified query.
        dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

        // Create a command builder to generate SQL update, insert, and
        // delete commands based on selectCommand. These are used to
        // update the database.
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // Populate a new data table and bind it to the BindingSource.
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dataAdapter.Fill(table);
        bindingSource1.DataSource = table;

        // Resize the DataGridView columns to fit the newly loaded content.
        dataGridView1.AutoResizeColumns( 
            DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
    }
    catch (SqlException)
    {
        MessageBox.Show("To run this example, replace the value of the " +
            "connectionString variable with a connection string that is " +
            "valid for your system.");
    }
}

private void Button1_Click(object sender, EventArgs e)
{
    // Bind the DataGridView to the BindingSource
    // and load the data from the database.
    dataGridView1.DataSource = bindingSource1;
    GetData("select * from Customers");
}

Once the data is bound to your grid, then you would provide the user some way to Filter, but you want them to search the data again. So you will want to use in the code some way to select which SQL you will run. So you could pass the SQL into GetData() as a string that you have set based on the users selection

private void Button1_Click(object sender, EventArgs e)
{
    // Bind the DataGridView to the BindingSource
    // and load the data from the database.
    dataGridView1.DataSource = bindingSource1;

    string sqlQuery = string.Empty;

    if(your check goes here)
    {
        sqlQuery = "select * from Customers";
    }
    else 
    {
        sqlQuery = "your new SQL";
    }

    GetData(sqlQuery);
}

then you would rebind your data based on the new query.

Upvotes: 1

Deb
Deb

Reputation: 991

Change your data source.

create a view with the join.

Use that view as your data source for data binding.

and use the filter if required.

Remember the filter becomes part of the where clause.

Upvotes: 1

Related Questions