我有2个组合框(Ex cb1,cb2),一个有机器组,另一个有机器。 当我选择cb1时它应该显示总数而不过滤cb2但是当我选择cb2时它应该过滤记录。一切正常但是默认文本没有显示在combobox2中(如总计或所有机器) 我的问题是当我选择combobox1时显示总数但是combobox2为空它应该显示包含总计/所有机器的文本我尝试了几种方法我不能这样做。
private void Form1_Load(object sender, EventArgs e)
{
try
{
this.dtStartDate.Value = DateTime.Now.AddDays(-3);
this.dtEndDate.Value = DateTime.Now;
PopulateGroupList();
cboMachines.Items.Insert(0, "-ALL MACHINES-");
cboMachines.SelectedIndex = 0;
//cboMachines.SelectedText = "Please Select One";
cboMachineGroup.SelectedText = "Please Select One";
//this.cboMachineGroup.SelectedIndex = 0;
PopulateMachineList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
btnShowForDateRange.Enabled = true;
}
private void PopulateMachineList()
{
cboMachines.Items.Clear();
string stringSql = "SELECT DISTINCT lfh.OriginalMachineName " +
"FROM MachineGroups mg " +
"INNER JOIN MachineGroupList mgl " +
"ON (mg.GroupID = mgl.GroupID) " +
"INNER JOIN LogFileHeader lfh ON (mgl.MachineID = lfh.FKMachineID) " +
"WHERE mg.Name=@groupname " +
"ORDER BY lfh.OriginalMachineName" ;
DataTable myDatatable = new DataTable();
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = connString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
myCommand.Parameters.Add("@groupname", SqlDbType.VarChar).Value = this.cboMachineGroup.Text;
myConn.Open();
SqlDataReader reader = myCommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
this.cboMachines.Items.Add(reader[0].ToString());
}
}
}
private void PopulateGroupList()
{
string stringSql = "SELECT DISTINCT mg.Name FROM MachineGroups mg ORDER BY Name";
DataTable myDatatable = new DataTable();
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = connString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
myConn.Open();
SqlDataReader reader = myCommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
this.cboMachineGroup.Items.Add(reader.GetString(0));
}
}
}
public DataTable GetDataByMachineGroupAndDateRange(string startDate, string endDate, string machineGroupname, string machineID)
{
startDate = startDate + " 00:00:00";
endDate = endDate + " 23:59:59";
string stringSql = "";
if (machineID.Length >0)
{
stringSql = "SELECT mg.GroupID, mg.Name, " +
"CONVERT(VARCHAR(10),rta.IDdatetime,121) AS date_only, " +
"CONVERT(VARCHAR(8), rta.IDdatetime, 108) AS time_only, " +
"rta.TicketPrice, rta.CardAmount, rta.FKTariffID, rta.RegistrationNum, rta.ExpiryDateTime, " +
"lfh.OriginalMachineName " +
"FROM MachineGroups mg " +
"INNER JOIN MachineGroupList mgl " +
"ON (mg.GroupID = mgl.GroupID) " +
"INNER JOIN LogFileHeader lfh ON (mgl.MachineID = lfh.FKMachineID) " +
"INNER JOIN RegistrationTransactionData rta ON (lfh.PKId = rta.FKLogFileHeader) " +
"WHERE mg.Name=@groupname AND lfh.OriginalMachineName=@machineid " +
"AND rta.IDdatetime BETWEEN @startdate AND @enddate " +
"ORDER BY rta.IDdatetime" ;
}
else
{
stringSql = "SELECT mg.GroupID, mg.Name, " +
"CONVERT(VARCHAR(10),rta.IDdatetime,121) AS date_only, " +
"CONVERT(VARCHAR(8), rta.IDdatetime, 108) AS time_only, " +
"rta.TicketPrice, rta.CardAmount, rta.FKTariffID, rta.RegistrationNum, rta.ExpiryDateTime, " +
"lfh.OriginalMachineName " +
"FROM MachineGroups mg " +
"INNER JOIN MachineGroupList mgl " +
"ON (mg.GroupID = mgl.GroupID) " +
"INNER JOIN LogFileHeader lfh ON (mgl.MachineID = lfh.FKMachineID) " +
"INNER JOIN RegistrationTransactionData rta ON (lfh.PKId = rta.FKLogFileHeader) " +
"WHERE mg.Name=@groupname " +
"AND rta.IDdatetime BETWEEN @startdate AND @enddate " +
"ORDER BY rta.IDdatetime";
}
DataTable myDatatable = new DataTable();
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = connString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand;
myCommand.Parameters.Add("@groupname", SqlDbType.VarChar).Value = machineGroupname;
if (machineID.Length > 0)
{
myCommand.Parameters.Add("@machineid", SqlDbType.VarChar).Value = machineID;
}
myCommand.Parameters.Add("@startdate", SqlDbType.DateTime).Value = startDate;
myCommand.Parameters.Add("@enddate", SqlDbType.DateTime).Value = endDate;
myAdapter.Fill(myDatatable);
return myDatatable;
}
private void btnShowForDateRange_Click(object sender, EventArgs e)
{
tblDateRange = new DataTable();
this.btnShowForDateRange.Enabled = false;
this.btnFindVRM.Enabled = false;
this.progressBar1.Visible = true;
btnExport.Enabled = false;
this.cboMachines.SelectedText = "ALL Machines";
string[] parameters = { dtStartDate.Value.ToString("yyyy-MM-dd"), dtEndDate.Value.ToString("yyyy-MM-dd"), this.cboMachineGroup.Text, this.cboMachines.Text };
this.backgroundWorker1.RunWorkerAsync(parameters);
}
private void btnQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] parameters = (string[])e.Argument;
tblDateRange = GetDataByMachineGroupAndDateRange(parameters[0], parameters[1], parameters[2], parameters[3]);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.dataGridView1.DataSource = tblDateRange;
this.btnShowForDateRange.Enabled = true;
this.btnFindVRM.Enabled = true;
this.progressBar1.Visible = false;
this.lblCount.Visible = true;
this.lblCount.Text = "Count: " + tblDateRange.Rows.Count.ToString();
tblVRM = new DataTable();
btnExport.Enabled = true;
}
private void cboMachineGroup_SelectedIndexChanged(object sender, EventArgs e)
{
//this.cboMachines.Text = "";
PopulateMachineList();
}
private void cboMachines_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnFindVRM_Click(object sender, EventArgs e)
{
tblVRM = new DataTable();
this.btnShowForDateRange.Enabled = false;
this.btnFindVRM.Enabled = false;
this.progressBar1.Visible = true;
btnExport.Enabled = false;
string[] parameters = { txtVRM.Text };
this.backgroundWorker2.RunWorkerAsync(parameters);
}
public DataTable GetDataByVRM(string vrm_in)
{
string stringSql = "SELECT mg.GroupID, mg.Name, " +
"CONVERT(VARCHAR(10),rta.IDdatetime,121) AS date_only, " +
"CONVERT(VARCHAR(8), rta.IDdatetime, 108) AS time_only, " +
"rta.TicketPrice, rta.CardAmount, rta.FKTariffID, rta.RegistrationNum, rta.ExpiryDateTime, " +
"lfh.OriginalMachineName " +
"FROM MachineGroups mg " +
"INNER JOIN MachineGroupList mgl " +
"ON (mg.GroupID = mgl.GroupID) " +
"INNER JOIN LogFileHeader lfh ON (mgl.MachineID = lfh.FKMachineID) " +
"INNER JOIN RegistrationTransactionData rta ON (lfh.PKId = rta.FKLogFileHeader) " +
"WHERE rta.RegistrationNum=@vrm AND mg.Name NOT IN ('Mains','ALL','ANPR') " +
"ORDER BY rta.IDdatetime";
DataTable myDatatable = new DataTable();
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = connString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = stringSql;
myCommand.Connection = myConn;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = myCommand;
myCommand.Parameters.Add("@vrm", SqlDbType.VarChar).Value = vrm_in;
myAdapter.Fill(myDatatable);
return myDatatable;
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
string[] parameters = (string[])e.Argument;
tblVRM = GetDataByVRM(parameters[0]);
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.dataGridView1.DataSource = tblVRM;
this.btnShowForDateRange.Enabled = true;
this.btnFindVRM.Enabled = true;
this.progressBar1.Visible = false;
this.lblCount.Visible = true;
this.lblCount.Text = "Count: " + tblVRM.Rows.Count.ToString();
tblDateRange = new DataTable();
btnExport.Enabled = true;
}
答案 0 :(得分:0)
由于您清除了“PopulateMachines”中的ComboBox Items集合,您必须再次添加该项目
private void cboMachineGroup_SelectedIndexChanged(object sender, EventArgs e)
{
//this.cboMachines.Text = "";
PopulateMachineList();
cboMachines.Items.Insert(0, "-ALL MACHINES-");
cboMachines.SelectedIndex = 0;
}
第二次尝试:
private void btnShowForDateRange_Click(object sender, EventArgs e)
{
tblDateRange = new DataTable();
this.btnShowForDateRange.Enabled = false;
this.btnFindVRM.Enabled = false;
this.progressBar1.Visible = true;
btnExport.Enabled = false;
cboMachines.Items.Insert(0, "-ALL MACHINES-");
cboMachines.SelectedIndex = 0;
string[] parameters = { dtStartDate.Value.ToString("yyyy-MM-dd"), dtEndDate.Value.ToString("yyyy-MM-dd"), this.cboMachineGroup.Text, this.cboMachines.Text };
this.backgroundWorker1.RunWorkerAsync(parameters);
}