0

过滤器适用于 myTicketsSubmitButton,但不适用于 allTicketsSubmitButton。代码是相同的,但我不明白为什么它适用于一种方法而不适用于另一种方法。

我在 Visual Studio 2010 中使用 WinForms 和 C#

    private void myTicketsSubmitButton_Click(object sender, EventArgs e)
    {
        String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user WHERE u.CallerName = '" + Environment.UserName.ToLower() + "'";
        GetData(sqlQuery);

        if (myTicketsAllRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
        }

        if (myTicketsClosedRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
        }

        if (myTicketsOpenRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Open'";                
        }
    }

    private void allTicketsSubmitButton_Click(object sender, EventArgs e)
    {
        String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user";
        GetData(sqlQuery);

        if (myTicketsAllRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
        }

        if (myTicketsClosedRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
        }

        if (myTicketsOpenRadioButton.Checked)
        {
            //GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Open'";               
        }
    }

    private void GetData(string selectCommand) 
    {
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
        BindingSource bindingSource1 = new BindingSource();
        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 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\testdb.accdb";

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

            // Create a command builder to generate SQL update, insert, and 
            // delete commands based on selectCommand. These are used to 
            // update the database. 
            OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(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;
            ticketsBindingSource = bindingSource1;

            // Resize the DataGridView columns to fit the newly loaded content. 
            //ticketsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);                

            ticketsDataGridView.DataSource = bindingSource1;
        }
        catch(OleDbException)
        {
            MessageBox.Show("To run this example, replace the value of the connectionString variable with a connection string that is valid for your system.");
        }
    }

我有 6 个单选按钮。

其中 3 个用于 myTickets --Open --Closed --All

其中 3 个用于 allTickets --Open --Closed --All

当我单击 myTickets 组的单选按钮时,一切正常

我对代码做了一些小改动,但 allTicketsSubmitButton 没有显示 Open 或 All 票的所有票。

数据库相对较小,因此我可以随时快速测试。

我有 5 个条目 2 打开 2 已关闭 1 正在进行中

2 个条目分配给另一个用户(因此其中 3 个是 myTickets)。

我注意到了一些奇怪的事情

仅当我将 myTickets 和 allTickets 的单选按钮设置为相同的东西时,结果才能正确显示

(两个打开都会显示打开的票)如果一个是打开的,另一个是关闭的,那么什么都不会发生

4

1 回答 1

0

如果您有 6 个单选按钮,3 个用于 myTickets,3 个用于 allTickets,那么为什么要在 allTickets 按钮单击事件下检查您的 myTickets?

我认为您需要将代码从以下位置更改:

private void allTicketsSubmitButton_Click(object sender, EventArgs e)
    {
        String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user";

        if (myTicketsAllRadioButton.Checked)
        {
            GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
        }

        if (myTicketsClosedRadioButton.Checked)
        {
            GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
        }

        if (myTicketsOpenRadioButton.Checked)
        {
            GetData(sqlQuery);
            ticketsBindingSource.Filter = "ProblemStatus = 'Open'";               
        }
    }

至:

private void allTicketsSubmitButton_Click(object sender, EventArgs e)
    {
        String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user";
        GetData(sqlQuery); // move this here, you only need this in one place

        if (allTicketsAllRadioButton.Checked)
        {
            ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
        }

        if (allTicketsClosedRadioButton.Checked)
        {
            ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
        }

        if (allTicketsOpenRadioButton.Checked)
        {
            ticketsBindingSource.Filter = "ProblemStatus = 'Open'";               
        }
    }
于 2012-04-17T17:48:56.110 回答