ListBox与多列绑定 [英] ListBox Binding with multiple Column

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

问题描述

嗨.....我有ListBox的Window窗体。我想用多个Column绑定ListBox。请帮助我。



query =选择Fname ,Lname来自联系方式;



我想在ListBox中看到Fname,Lname,帮帮我怎样才能做到这一点..?

解决方案

用烘焙的代码解答你的答案,

说明:在页面加载中,我已将数据库中的数据添加到列表框中。



 private void Update_Existing_Load(object sender,EventArgs e)
{
//我已经清除了列表框,因为如果你想打算打电话任何其他事件上的load事件然后列表框中的数据出现两次因此你必须首先清除它
listBox1.Items.Clear();
using(SqlConnection con = new SqlConnection())
{
con.ConnectionString =Data Source = .; Initial Catalog = phonebook; Integrated Security = True;
con.Open();
string query =select FirstName +''''+ LastName from Details;
SqlCommand cmd = new SqlCommand(query,con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read()== true)
{
//将数据添加到列表框
listBox1.Items.Add(dr [0] .ToString());
}
//关闭连接
con.Close();
//释放占用的资源
con.Dispose();
}

}



一旦发现它有用,请评价我的答案

谢谢&问候

Radix:rose:


你可以写下你的查询



 query =选择Fname +'',''+ Lname作为ContactName来自联系人; 





将此加载到数据集并绑定如下。

 yourListBox.DataSource = dataSet1.Tables [0]; 
yourListBox.DisplayMember =ContactName;


请参阅msdn示例这里

Hi.....I have Window form with ListBox.I want to Bind ListBox with multiple Column.Please Help Me.

query="Select Fname,Lname From Contact";

I want to see Fname,Lname In ListBox ,help me How can I do this..?

解决方案

Hers your answer with the baked code,
Explanation: Here on the page load i have added the data from the database into the listbox.

 private void Update_Existing_Load(object sender, EventArgs e)
        {
//i have cleared the listbox because if u wish to plan on calling the load event on any other event then the data in the list box appears twice hence u have to clear it first
            listBox1.Items.Clear();
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = "Data Source=.;Initial Catalog=phonebook;Integrated Security=True";
                con.Open();
                string query = "select FirstName+'' ''+LastName from Details";
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read() == true)
                {
//adding data to listbox 
                    listBox1.Items.Add(dr[0].ToString());
                }
//close the connection
                con.Close();
//releasing the occupied resource
                con.Dispose();
            }

        }


Do rate my answer once you find it useful
Thanks & Regards
Radix :rose:


You can write your query like following

query = "Select Fname + '', '' + Lname as ContactName From Contact";



Load this to a dataset and bind like following.

yourListBox.DataSource = dataSet1.Tables[0];
yourListBox.DisplayMember = "ContactName";


Please see the msdn sample here.


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

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