Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am doing application using List box in c sharp in windows application.

Design as follows;

Listbox1 Buttons Listbox2

Ram ADD
Sam ADD ALL
Suresh Remove
Vignesh Remove All
Load
Clear

SqlConnection con = new SqlConnection(" Data Source=INDIA;Initial Catalog=HIMTTESTing;Integrated Security=True");

Code as follows for button(Load)
C#
private void Btn_Load_Click(object sender, EventArgs e)
{
  string sql = "select Faculty_Name from  Tb_SCH_Faculty_List";
  DataSet mydataset = new DataSet();
  SqlDataAdapter adp = new SqlDataAdapter(sql, con);
  adp.Fill(mydataset,"Tb_SCH_Faculty_List");
  DataTable mydatatable  = mydataset.Tables[0];
  DataRow temprow = null;

  foreach (DataRow tempRow_Variable in mydatatable.Rows)
  {
   temprow  = tempRow_Variable;
   Lb_Faculty_Name.Items.Add((temprow["Faculty_Name"]));
  }
}


Code as follows for button(ADD ALL)
C#
private void Btn_Add_All_Click(object sender, EventArgs e)
{
  string com = " Select Faculty_Name from Tb_SCH_Faculty_List";
  DataSet mydataset = new DataSet();
  SqlDataAdapter adpt = new SqlDataAdapter(com, con);
  adpt.Fill(mydataset, "Tb_SCH_Faculty_List");
  DataTable mydatatable = mydataset.Tables[0];
  DataRow temprow = null;

  foreach (DataRow tempRow_Variable in mydatatable.Rows)
  {
    temprow = tempRow_Variable;
    Lb_Selected_Faculty.Items.Add((temprow["Faculty_Name"]));
  }
}


Both Load and ADD All Button working fine.

Output shows as;
when i click the Load (Button)all faculty name is retrieved from the data base and displayed in the Lisbox1.

similarly when i click the ADD ALL (Button)all faculty name is displayed in the Lisbox2.

the above two buttons Load and ADD ALL is working fine.
in the ADD button;

when i click the Load button all names are retrieved from the data base and displayed in the Listbox1,in the Listbox1 when i select a Particular name and click the ADD button the particular name want to display in to the Listbox2.

for the particular name displaying how to do.please help me.

/Edit Pre tag added by Jibesh
Posted
Updated 13-Jan-13 20:15pm
v2

1 solution

The way you handled the presentation layer doesnt look good. since your UI should move the items selected from the first listbox to the second list box, you dont need to go for database operation unless you are doing any other checking.

Just fill the ListBox1 from database and use the ListBox properties for further operations. The normal steps followed to my belief are

1.Fill the ListBox1 from database
2.Select top item in the list box as selected using lstBox1.SelectedIndex = 0;
3.Select the item(s) from the listBox1
4.OnPressing the AddAll button iterate the selected Items using the lstBox1.SelectedItems property and add to the listbox2 using listBox2.Items.Add(..) method
5.Remove the moved item from the listBox1


let me know if you want know more.
 
Share this answer
 
v2

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