本文介绍了列表框空未显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 亲爱的所有人, i我正在使用vs2008和sql数据库。 i有一个组合框,其中有有两种选择:黄金和白银 当我选择白银价值然后它显示数据库中的白银价值,如果我选择黄金则其显示数据库中的黄金价值。 /> 但它无效。 DAtabase:RPSJDB 表名:ItemTable 列名:txtItemId(pk,int,NotNull) cmbItemType(nvarcha(50),不是Null) txtItemCode(nvarcha(50),而不是Null) txtItemDescription(nvarcha(50),而不是Null) txtLaborCharge(float,Null) txtItemPercent(float,Null) 当itemType = Gold时,然后所有Itemdescription =在列表框中显示 所有编码都是工作正常,没有任何错误。请告诉我我的代码中有什么问题。 private void cmboItemType()// for combobox { string connstr = @Server = .\SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True;最大池大小= 100; SqlConnection conn = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand(SELECT Distinct * FROM ItemTable,conn); // SqlCommand cmd = new SqlCommand(SELECT DISTINCT cmbItemType FROM ItemTable,conn); conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(); ArrayList ItemStore = new ArrayList(); while(sdr.Read()) { ItemStore.Add(new AddValue(sdr.GetString(1),sdr.GetInt32(0))); } sdr.Close(); conn.Close(); cmbItemType.DataSource = ItemStore; cmbItemType.DisplayMember =Display; this。 cmbItemType.ValueMember =Value; ItemHaveBeenAdded = true; } public class AddValue { private string Displa YM; private long Valm; public AddValue(string Display,long Value) { Displaym = Display; Valm =价值; } 公共字符串显示 { get {return Displaym; } } public long Value { get {return Valm; } } } private void cmbItemType_SelectedIndexChanged(object sender,EventArgs e) { if(this.ItemHaveBeenAdded) lstboxItem( ); } private void lstboxItem() { string connstr = @Server = .\SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True; Max Pool Size = 100 ; SqlConnection conn = new SqlConnection(connstr); this.lstbItemDescription.Items.Clear(); SqlCommand cmd = new SqlCommand (SELECT cmbItemType FROM ItemTable where cmbItemType =+ cmbItemType.SelectedValue,conn); conn.Open(); SqlDataReader sdrItem = cmd.ExecuteReader(); while(sdrItem.Read()) { this.lstbItemDescription.Items.Add(sdrItem.GetString(1)); } sdrItem.Close(); conn.Close(); } private void button1_Click(object sender,EventArgs e) { this.Close(); } 解决方案 Dear All,i am using vs2008 and sql database.i have a combobox in which there are two option : gold and silver when i am select the silver value then its show the silver value from the database and if i am select the gold then its show the gold value from the database.but its not working.DAtabase :RPSJDB Table Name: ItemTableColumn Name: txtItemId(pk,int,NotNull) cmbItemType(nvarcha(50),not Null) txtItemCode(nvarcha(50),not Null) txtItemDescription(nvarcha(50),not Null) txtLaborCharge(float,Null) txtItemPercent(float,Null) when itemType=Gold then all the Itemdescription = show in list boxall the coding is working fine without any error.so pls tell me what is the problem in my code.private void cmboItemType() //for combobox { string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100"; SqlConnection conn = new SqlConnection(connstr); SqlCommand cmd = new SqlCommand("SELECT Distinct * FROM ItemTable", conn); //SqlCommand cmd = new SqlCommand("SELECT DISTINCT cmbItemType FROM ItemTable", conn); conn.Open(); SqlDataReader sdr = cmd.ExecuteReader(); ArrayList ItemStore = new ArrayList(); while (sdr.Read()) { ItemStore.Add(new AddValue(sdr.GetString(1), sdr.GetInt32(0))); } sdr.Close(); conn.Close(); cmbItemType.DataSource = ItemStore; cmbItemType.DisplayMember = "Display"; this.cmbItemType.ValueMember = "Value"; ItemHaveBeenAdded = true; } public class AddValue { private string Displaym; private long Valm; public AddValue(string Display,long Value) { Displaym = Display; Valm = Value; } public string Display { get { return Displaym; } } public long Value { get { return Valm; } } } private void cmbItemType_SelectedIndexChanged(object sender, EventArgs e) { if (this.ItemHaveBeenAdded) lstboxItem(); } private void lstboxItem() { string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100"; SqlConnection conn = new SqlConnection(connstr); this.lstbItemDescription.Items.Clear(); SqlCommand cmd = new SqlCommand ("SELECT cmbItemType FROM ItemTable Where cmbItemType =" + cmbItemType.SelectedValue, conn); conn.Open(); SqlDataReader sdrItem = cmd.ExecuteReader(); while (sdrItem.Read()) { this.lstbItemDescription.Items.Add(sdrItem.GetString(1)); } sdrItem.Close(); conn.Close(); } private void button1_Click(object sender, EventArgs e) { this.Close(); } 解决方案 这篇关于列表框空未显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 14:54