Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..?
Posted

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:
 
Share this answer
 
Comments
Member 11788122 12-Oct-15 1:42am    
Thanks,this code working good for me.
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";
 
Share this answer
 
Comments
Member 11788122 12-Oct-15 1:36am    
but there is no method like DisplayMember for listbox.
Please see the msdn sample here.
 
Share this answer
 
hello
may this will help you



C#
try
          {
              con = new MySqlConnection();
              con.ConnectionString = ConfigurationSettings.AppSettings["constr"];
              con.Open();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          string str = "select CONCAT(SNAME, ' ' ,SCODE) AS mixcode from stationcode;";
          da = new MySqlDataAdapter(str, con);
          ds = new DataSet();
          //da.TableMappings.Add("table", "stationcode");
          da.Fill(ds, "stationcode");
          this.comboBox1.DataSource = this.ds;
          this.comboBox1.DisplayMember = "stationcode.mixcode";
          this.comboBox1.ValueMember = "stationcode.mixcode";


Happy to help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900