过滤器适用于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 - 打开 - 关闭 - 所有
其中3个适用于所有门票 - 打开 - 关闭 - 所有
单击myTickets组的单选按钮时,一切正常
我对代码做了一些小改动,但是allTicketsSubmitButton没有显示Open或All门票的所有门票。
数据库相对较小,所以我可以随时快速测试。
我有5个条目 2打开 2关闭 1正在进行中
2个条目被分配给另一个用户(因此其中3个是myTickets)。
我发现了一些奇怪的东西
如果我将myTickets和allTickets的单选按钮设置为相同的东西,结果只能正确显示
(两个打开都会显示打开的门票) 如果一个是开放的而另一个是关闭的,则没有任何反应
答案 0 :(得分: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'";
}
}
TO:
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'";
}
}