我搜索了類似的問題,但沒有一個適用於我的情況。 所以我有一個列表框女巫應該填充數據取決於從組合框中選擇的項目。 代碼工作正常,但由於軟件中的一些變化,我不得不創建一個新表單,將設計和代碼複製/粘貼到新表單。 我做了必要的調整,但現在,所有的組合框填充和列表框不會。 可以anyoe說爲什麼,代碼:c#listbox不填充
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);
}
}
如果所有東西都一樣,並且您複製了它,您有可能忘記將事件處理程序連接到組合框? – EpicKip
@EpicKip嗨,就是這樣,謝謝,請回答並生病選擇你的文章作爲答案。 – septaug
很高興這是一個簡單的修復,爲那些重新創建其形式的人發佈了一個通用的答案 – EpicKip