本文介绍了从列表框中移动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个列表框说A,B和C. A和B绑定了相同的数据源。如果我从列表框A中选择项目并移动到列表框C,则该项目应该从列表框B中隐藏<再次当我将同一项目从列表框C移动到列表框A然后它应取消隐藏列表框B中的相同项目

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

推荐答案


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

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();
      }



在BtnForward中,单击lbx1选择的项目添加到lbx3并从lbx1和lbx2中删除


In BtnForward click lbx1 selected item added to lbx3 and removed from lbx1 and lbx2

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);
   }



在BtnBackward点击方法lbx3所选项目将被添加到lbx1和lbx2,因为它们

出现在同一位置: -


In BtnBackward Click Method lbx3 selected item will be added to lbx1 and lbx2 as they
were appear at same position:-

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!!!


这篇关于从列表框中移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:28