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