3
Answers

How to bind the one column data in Listbox control in windows form applications?

ajay raju

ajay raju

14y
5.9k
1

hi!
 i am using listbox control to bind the one column data.  in sql server i take one table like Emp. in that table "Ename"  column data is bind into the listbox control. i done in web application. but in windows applications how to write the code to do this. i write code in web applications  like.
listbox1.datasource = ds.tables[0];
listbox1.datatextfield = "Ename";
listbox1.databind();
Please give a code to do this.
thanks. 
Answers (3)
0
Vasanth

Vasanth

NA 2.3k 309.6k 14y


Hi Ajay ,
 
Here just we cast from checkedlistbox to listbox .
 
 
//Create temp datatable for bind values

DataTable dtTemp = new DataTable(); DataRow dr;
//create schema
dtTemp.Columns.Add("Eid"); dtTemp.Columns.Add("Ename");
//Add temp datas
dr = dtTemp.NewRow(); dr["Eid"] = "1"; dr["Ename"] = "CAAA"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Eid"] = "2"; dr["Ename"] = "CBBB"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Eid"] = "3"; dr["Ename"] = "CCCC"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Eid"] = "4"; dr["Ename"] = "CDDD"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Eid"] = "5"; dr["Ename"] = "CEEE"; dtTemp.Rows.Add(dr);

//Just u cast here
(checkedListBox1 as ListBox).DataSource = dtTemp;
(checkedListBox1 as ListBox).DisplayMember = "Ename";
(checkedListBox1 as ListBox).ValueMember = "Eid";
 
 
 
Please mark as answer , if it helps you.
0
ajay raju

ajay raju

NA 384 0 14y

hi! Vasanth Thank u for giving Replay. if u don't mine also give acode for doing data bind in CheckedListBox in Windows Application.


i am waiting for ur replay.
Thank u.
0
Vasanth

Vasanth

NA 2.3k 309.6k 14y

As like same , please check below.  

  listbox1.DataSource = ds.Tables["<name>"];
  listbox1.DisplayMember = "Ename";

 
Please mark as answer , if it helps you.