简体   繁体   中英

C# winform How to filter combobox if the specific column is not null

For example I have a Project Table:

ProjectID | ProjectName | EstimateStartDate | EstimateEndDate | ActaulStartDate | ActualEndDate |  
    1     |Liberty Green|    06-26-2017     |   06-30-2017    |    06-26-2017   |   08-30-2017  |
    2     |   Wharton   |    06-26-2017     |   06-30-2017    |       null      |   null        |

So basically I want that all the rows Where ActualStartDate and ActualEndDate are null to be show in the ComboBoxProjectName, and all the not null will not show anymore in the ComboBoxProjectName.

you can try select all the rows where ActualStartDate is null... and then set datasource combobox by datatable :

SqlConnection con = new SqlConnection("ConnectionString");
        try
        {
            if (con.State == ConnectionState.Closed) con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from Project Table where ActaulStartDate is null and ActualEndDate is null";
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "ProjectName";
            comboBox1.ValueMember = "ProjectID";
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (con.State == ConnectionState.Open) con.Close();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM