使用SQL的combobox / SelectedIndexChanged

时间:2012-12-20 21:11:05

标签: c# sql visual-studio combobox

我有3个不同的SQL代码。我把我的3个文本(男人,女人,两个)放在组合框中。我不知道如何在组合框中选择示例Men,好的SQL代码适用。

男士:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
  AND Gender = 'M' 
ORDER BY LastName, FirstName

女人:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
  AND Gender = 'F' 
ORDER BY LastName, FirstName

两者:

SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender 
FROM Employes, Contacts 
WHERE Employes.ContactId = Contacts.ContactID 
ORDER BY LastName, FirstName

看(法语)(Choisir le sexe =选择性别)(Afficher =英语显示)

enter image description here

非常感谢!

1 个答案:

答案 0 :(得分:1)

假设您使用三个字符串Hommes, Femmes, Tous填充组合框,那么当您收到事件SelectedIndexChanged时,您可以编写类似这样的内容

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox.
   string curItem = comboBox1.SelectedItem.ToString();
   string sqlText = "SELECT EmployeeID, FirstName, LastName, MaritalStatus, Gender " + 
                    "FROM Employes JOIN Contacts ON Employes.ContactId = Contacts.ContactID "
   switch(curItem)
   {
      case "Femmes":
          sqlText += "AND Gender = 'F' ORDER BY LastName, FirstName";
          break;
      case "Hommes":
          sqlText += "AND Gender = 'M' ORDER BY LastName, FirstName";
          break;
      default:
          sqlText += "ORDER BY LastName, FirstName";
   }
   DataTable dt = GetEmployeeList(sqlText);
   ...... // Now fill the datasource of your grid
}
编辑:呃,只需阅读@marc_s评论