绑定源筛选器不起作用
本文关键字:不起作用 筛选 绑定 | 更新日期: 2023-09-27 18:31:20
过滤器适用于myTicketsSubmitButton,但不适用于allTicketsSubmitButton。代码是相同的,但我不明白为什么它适用于一种方法而不是另一种方法。
我正在使用WinForms和C#与Visual Studio 2010
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 个用于我的门票--打开--闭--都
其中 3 张适用于所有门票--打开--闭--都
当我单击myTickets组的单选按钮时,一切正常
我对代码做了一个小的更改,但allTicketsSubmitButton没有显示打开或所有票证的所有票证。
数据库相对较小,因此我可以随时快速测试内容。
我有 5 个条目2 打开2 已关闭1 进行中的工作
2个条目被分配给另一个用户(因此其中3个是myTickets)。
我注意到一些奇怪的事情
仅当我将我的门票和所有门票的单选按钮设置为同一事物时,结果才会正确显示
(两个打开的都会显示打开的门票)如果一个是打开的,另一个是关闭的,那么什么都不会发生
如果您有 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'";
}
}