Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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)
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)
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 13-Jan-13 21:08pm
Edited 13-Jan-13 21:15pm
Jibesh16.6K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100