使用内部联接的BindingSource.Filter

时间:2012-04-12 14:35:51

标签: sql datagridview inner-join bindingsource

我正在使用BindingSource,我想使用一些SQL代码来执行内部联接。 我的代码没有用

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'";

但以下工作

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

如何运行InnerJoin查询并更新我的datagridview?

2 个答案:

答案 0 :(得分:1)

BindingSource.Filter将过滤器应用于已加载到DataGridView的数据。因此,如果您显示了帐户,并且您正在寻找一个特定帐户,那么您将按帐号过滤该数据。

Filter,无法为您运行另一个SQL查询。

如果您要对该数据执行INNER JOIN,则需要执行其他搜索并加载DataGridView。听起来您不想执行过滤器,但是您希望在同一网格中的数据上显示两个单独的集合。

您的基本流程是:

  1. 加载DataGridView
  2. 使用选择他们想要的过滤器或记录
  3. 使用您的INNER JOIN执行第二次搜索
  4. 使用新数据重新加载DataGridView。
  5. 编辑这里是一些可能让你开始的代码:

    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");
    }
    

    一旦数据绑定到您的网格,那么您将为用户提供一些过滤方法,但您希望他们再次搜索数据。因此,您需要在代码中使用某种方式来选择要运行的SQL。因此,您可以将SQL作为基于用户选择

    设置的字符串传递给GetData()
    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);
    }
    

    然后您将根据新查询重新绑定数据。

答案 1 :(得分:1)

更改您的数据源。

使用连接创建视图。

使用该视图作为数据绑定的数据源。

并根据需要使用过滤器。

请记住,过滤器成为where子句的一部分。