c#listbox没有填写

时间:2017-02-13 13:53:03

标签: c# sql combobox listbox

我搜索了类似的问题,但没有一个适用于我的情况。 所以我有一个列表框女巫应该填充数据取决于组合框中的选定项目。 代码工作正常,但由于软件中的一些更改我必须创建一个新表单,将设计和代码复制/粘贴到新表单。 我做了必要的调整,但现在,所有的组合框都填满了,而列表框也没有。 anyoe可以说为什么,代码是:

using System;
using System.Data;
using System.Windows.Forms;
using XXXXX.bin;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;


namespace XXXXX
{
    public partial class vidro : Form
    {
        public static SqlConnection con = Globais.GetDbConection();
        public vidro()
        {
            InitializeComponent();
        }
        private void vidro_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 desempenho_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillData();
        }
        private void valu_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillData();
        }
        private void fsolar_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillData();
        }
        private void selec_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillData();
        }
        private void compo_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillData();
        }
        private void FillData()
        {
            string combo1value = desempenho.Text;
            string combo2value = valu.Text;
            string combo3value = fsolar.Text;
            string combo4value = select.Text;
            string combo5value = compo.Text;

            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);
            }
        }

2 个答案:

答案 0 :(得分:3)

因为您将代码转移到新表单,所以需要为控件挂钩事件处理程序。

这可以通过选择控件并转到其事件选项卡(属性附近的闪电形状)来完成 enter image description here

或者在代码中执行:controlName.EventName += eventHandlerMethodName;
示例:button1.Click += button1_Click;

答案 1 :(得分:2)

确保控件名称(ComboBox和Listbox)与复制的代码中的名称相同,然后为每个控件添加eventlisteners。