Click here to Skip to main content
15,028,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three listbox say A , B and C. A and B binded with same datasource. If i select item from listbox A and moved to listbox C then that item should hide from listbox B< and again when i moved same item from listbox C to list box A then it should unhide the same item in list box B
Posted

In your .aspx page add three listboxes and name it to lbx1,lbx2 and lbx3.
Add two buttons btnforward n btnbackward.
Write a function to fetch data from database
C#
public DataTable adddata()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string str = "select loguser from OrderStatususermaster where userid<'10'";
            cmd = new SqlCommand(str, con);
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

In page load method add
C#
if (!IsPostBack)
      {
          DataTable dt1 = adddata();
          for (int i = 0; i &lt; dt1.Rows.Count; i++)
          {
              lbx1.Items.Add(dt1.Rows[i][&quot;loguser&quot;].ToString().Trim());
              lbx2.Items.Add(dt1.Rows[i][&quot;loguser&quot;].ToString().Trim());
              lbx3.Items.Add(dt1.Rows[i][&quot;loguser&quot;].ToString().Trim());
          }
          GridView1.DataSource = dt;
          GridView1.DataBind();
      }

In BtnForward click lbx1 selected item added to lbx3 and removed from lbx1 and lbx2
C#
protected void btnforward_Click(object sender, EventArgs e)
   {
       string s1 = lbx1.SelectedItem.Text.Trim();
       lbx1.Items.Remove(s1);
       lbx2.Items.Remove(s1);
       lbx3.Items.Add(s1);
   }

In BtnBackward Click Method lbx3 selected item will be added to lbx1 and lbx2 as they
were appear at same position:-
C#
protected void btnbackward_Click(object sender, EventArgs e)
   {
       string s1 = lbx3.SelectedItem.Text.Trim();
       int index = 0;
       DataTable dt1 = new DataTable();
       dt1 = adddata();
       for (int i = 0; i &lt; dt1.Rows.Count; i++)
       {
           if (dt1.Rows[i][&quot;loguser&quot;].ToString().Trim() == s1)
               index = i;
       }
       lbx3.Items.Remove(s1);
       if (index &lt; lbx1.Items.Count)
       {
           lbx1.Items.Insert(index, s1);
           lbx2.Items.Insert(index, s1);
       }
       else
       {
           lbx1.Items.Add(s1);
           lbx2.Items.Add(s1);
       }
   }


Thank You!!!
   

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