c#用来自sql的数据填充listview

时间:2017-02-20 11:45:35

标签: c# sql listview

我在c#中有一个winform应用程序,它根据它们之间的关系将项目从comboboxes加载到listview。 那部分工作正常。 现在更简单的部分我无法工作,当我加载表单时,我希望列表框显示所有结果,并且只有当用户希望他可以使用组合框过滤时。 我怎样才能做到这一点。 简单的解释是: 如何在formload上将项目加载到listview? 我尝试了几种方法,但不会工作。 我删除了部分代码以缩短帖子。 我的代码是:

public test()
{
    InitializeComponent();
}  
private void test_Load(object sender, EventArgs e)
{
    SqlDataAdapter SDA = new SqlDataAdapter("select distinct desempenho from vidros", con);
    DataTable DTT = new DataTable();
    SDA.Fill(DTT);
    desempenho.Items.Clear();
    foreach (DataRow ROW in DTT.Rows)
    {
        desempenho.Items.Add(ROW["desempenho"].ToString());
    }
    SqlDataAdapter SDA2 = new SqlDataAdapter("select distinct valu from vidros", con);
    DataTable DTT2 = new DataTable();
    SDA2.Fill(DTT2);
    valu.Items.Clear();
    foreach (DataRow ROW in DTT2.Rows)
    {
        valu.Items.Add(ROW["valu"].ToString());
    }
    SqlDataAdapter SDA3 = new SqlDataAdapter("select distinct fs from vidros", con);
    DataTable DTT3 = new DataTable();
    SDA3.Fill(DTT3);
    fsolar.Items.Clear();
    foreach (DataRow ROW in DTT3.Rows)
    {
        fsolar.Items.Add(ROW["fs"].ToString());
    }
    SqlDataAdapter SDA4 = new SqlDataAdapter("select distinct sel from vidros", con);
    DataTable DTT4 = new DataTable();
    SDA4.Fill(DTT4);
    select.Items.Clear();
    foreach (DataRow ROW in DTT4.Rows)
    {
        select.Items.Add(ROW["sel"].ToString());
    }
    SqlDataAdapter SDA5 = new SqlDataAdapter("select distinct compo from vidros", con);
    DataTable DTT5 = new DataTable();
    SDA5.Fill(DTT5);
    select.Items.Clear();
    foreach (DataRow ROW in DTT5.Rows)
    {
        compo.Items.Add(ROW["compo"].ToString());
    }
    SqlDataAdapter SDA6 = new SqlDataAdapter("select distinct sel from vidros", con);
    DataTable DTT6 = new DataTable();
    SDA6.Fill(DTT6);
    select.Items.Clear();
    foreach (DataRow ROW in DTT6.Rows)
    {
        select.Items.Add(ROW["sel"].ToString());
    }
}
private void compo_SelectedIndexChanged_1(object sender, EventArgs e)
{
    FillData();
}
private void select_SelectedIndexChanged(object sender, EventArgs e)
{
    FillData();
}
private void fsolar_SelectedIndexChanged_1(object sender, EventArgs e)
{
    FillData();
}
private void valu_SelectedIndexChanged_1(object sender, EventArgs e)
{
    FillData();
}
private void desempenho_SelectedIndexChanged_1(object sender, EventArgs e)
{
    FillData();
}
 string query = "select [desc],[enchimento],[compo] from vidros where 1=1 ";
            string queryWhere = "";
            SqlDataAdapter sda = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            sda.SelectCommand = cmd;

            if (combo1value != "")
            {
                queryWhere += " and desempenho = @emp ";
                sda.SelectCommand.Parameters.Add("@emp", SqlDbType.NVarChar).Value = combo1value;
            }
            if (combo2value != "")
            {
                queryWhere += " and valu = @emp2 ";
                sda.SelectCommand.Parameters.Add("@emp2", SqlDbType.NVarChar).Value = combo2value;
            }
            if (combo3value != "")
            {
                queryWhere += " and fs = @emp3 ";
                sda.SelectCommand.Parameters.Add("@emp3", SqlDbType.NVarChar).Value = combo3value;
            }
            if (combo4value != "")
            {
                queryWhere += " and sel = @emp4 ";
                sda.SelectCommand.Parameters.Add("@emp4", SqlDbType.NVarChar).Value = combo4value;
            }
            if (combo5value != "")
            {
                queryWhere += " and compo = @emp5 ";
                sda.SelectCommand.Parameters.Add("@emp5", SqlDbType.NVarChar).Value = combo5value;
            }

            sda.SelectCommand.CommandText = query + queryWhere;

            DataTable DTT = new DataTable();

            sda.Fill(DTT);
            listView1.Items.Clear();
            for (int i = 0; i < DTT.Rows.Count; i++)
            {
                DataRow dr = DTT.Rows[i];
                ListViewItem listitem = new ListViewItem(dr["desc"].ToString());
                listitem.SubItems.Add(dr["enchimento"].ToString());
                listitem.SubItems.Add(dr["compo"].ToString());
                listView1.Items.Add(listitem);
            }

... 提前致谢

0 个答案:

没有答案