列表框选定的项目 [英] Listbox selected items

查看:57
本文介绍了列表框选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在开发winforms应用程序,



listbox是多选列表框。



填充包含数据源的列表框,具体取决于列表框中的选定值,

i需要从数据库中获取数据,



如何解决它。



< pre lang =   c# >  使用(MySqlConnection con =  new  MySqlConnection(ConnectionString))
{
string query = 从客户选择不同的area_code;
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(query,con);
DataTable dt = new DataTable();
da.Fill(dt);
listBox1.DataSource = dt;
listBox1.DisplayMember = area_code;

}







列表框显示正常,如下:11

33

44



当我选择11,33



i需要一个查询来从数据库中选择数据。



我正在使用:

< pre lang =   c# >   foreach 对象项目  listBox1.Items )
{
text + = item.ToString()+ ;
}

area = text;





和查询是:

query1 =select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code =+ area +;

解决方案

首先,您应该像这样构建区域临时字符串:

  string  area =  string  .Empty; 
foreach 对象项目 listBox1.Items)
{
if (area.Length > < span class =code-digit> 0 )area + = ;
area + = item.ToString();
}



然后使用区域字符串构建您的查询:



 query1 =   select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount ,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code in( + area +  ; 


Hi,

am developing winforms application,

listbox is multiselection listbox.

am filling listbox with datasource, depending on selected values from listbox,
i need to get data from database,

how to solve it.

<pre lang="c#"> using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                string query = "select distinct area_code from customer";
                con.Open();
                MySqlDataAdapter da = new MySqlDataAdapter(query,con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "area_code";
            
            }




listbox is displaying fine as ex:11
33
44

when i select 11, 33

i need a query to select data from database.

am using:

<pre lang="c#"> foreach (Object item in listBox1.Items)
            {
                text += item.ToString() + " ";
            }

            area = text;



and query is:
query1 = "select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code="+area+" ";

解决方案

First you should build your area temp string like this:

string area = string.Empty;
foreach (Object item in listBox1.Items)
{
 if(area.Length > 0) area += ", ";
 area += item.ToString();
}


Then to use the area string to build your query:

query1 = "select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code in ("+area+")";


这篇关于列表框选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆