Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to pass values from one listbox to another listbox in winform

What I have tried:

C#
private void btnMovetoList_Click(object sender, EventArgs e)
        {
            listBox2.SelectionMode = SelectionMode.MultiSimple;
            try
            {
                if (listBox1.Items.Count > 0)
                {
                    DataRowView drv = null;
                    for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                    {
                        drv = (DataRowView)listBox1.SelectedItems[i];

                        string valueOfItem = drv["Form_Name"].ToString();
                        //string  Item = drv["Form_No"].ToString();
                        listBox2.Items.Add(valueOfItem);
                        //listBox1.Items.Remove(Item);
                    }

                    
                    DataRowView rowView = listBox1.SelectedValue as DataRowView;

                    if (null == rowView)
                     {
                         return;
                     }

                    dt.Rows.Remove(rowView.Row);
                    dt.AcceptChanges();


                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            chkSelectAll1.Checked = false;
        }


        private void btnMoveReturn_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < listBox2.SelectedItems.Count; i++)
                {
                    String valueOfItem = listBox2.SelectedItems[i].ToString();
                    dt.Rows.Add(valueOfItem);
                    listBox2.SelectionMode = SelectionMode.MultiSimple;
                }
                for (int i = listBox2.SelectedIndices.Count - 1; i >= 0; i--)
                {
                    listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
                }
                chkSelectAll2.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 20-Mar-18 21:12pm
v2

1 solution

I solved, its working fine
C#
private void btnMovetoList_Click(object sender, EventArgs e)
        {
            listBox2.SelectionMode = SelectionMode.MultiSimple;
            try
            {
                List<object> _Objimp = new List<object>();
                List<object> _ObjExp = new List<object>();

                object _ObjimpList = new object();
                object _ObjExpList = new object();
                object _ObjStayItem = new object();

                if (listBox2.Items.Count > 0)
                {
                    for (int _listtwo = 0; _listtwo < listBox2.Items.Count; _listtwo++)
                    {
                        _ObjStayItem = new object();
                        _ObjStayItem = listBox2.GetItemText(listBox2.Items[_listtwo]);
                        _Objimp.Add(_ObjStayItem);
                    }
                }

                for (int _listtwoSelected = 0; _listtwoSelected < listBox1.SelectedItems.Count; _listtwoSelected++)
                {
                    _ObjimpList = new object();
                    _ObjimpList = listBox1.GetItemText(listBox1.SelectedItems[_listtwoSelected]);
                    _Objimp.Add(_ObjimpList);
                }

                for (int _listboxone = 0; _listboxone < listBox1.Items.Count; _listboxone++)
                {
                    _ObjExpList = new object();
                    if (!listBox1.GetSelected(_listboxone))
                    {
                        _ObjExpList = listBox1.GetItemText(listBox1.Items[_listboxone]);
                        _ObjExp.Add(_ObjExpList);
                    }
                }
                listBox1.DataSource = null;
                listBox1.Items.Clear();

                listBox2.DataSource = null;
                listBox2.Items.Clear();

                listBox1.DataSource = _ObjExp.OrderBy(r => r).ToList();
                listBox2.DataSource = _Objimp.OrderBy(p => p).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            chkSelectAll1.Checked = false;
        }
        private void btnMoveReturn_Click(object sender, EventArgs e)
        {
            try
            {
                List<object> _Objimp = new List<object>();
                List<object> _ObjExp = new List<object>();

                object _ObjimpList = new object();
                object _ObjExpList = new object();
                object _ObjStayItem = new object();

                if (listBox1.Items.Count > 0)
                {
                    for (int _listtwo = 0; _listtwo < listBox1.Items.Count; _listtwo++)
                    {
                        _ObjStayItem = new object();
                        _ObjStayItem = listBox1.GetItemText(listBox1.Items[_listtwo]);
                        _Objimp.Add(_ObjStayItem);
                    }
                }

                for (int _listtwoSelected = 0; _listtwoSelected < listBox2.SelectedItems.Count; _listtwoSelected++)
                {
                    _ObjimpList = new object();
                    _ObjimpList = listBox2.GetItemText(listBox2.SelectedItems[_listtwoSelected]);
                    _Objimp.Add(_ObjimpList);
                }

                for (int _listboxone = 0; _listboxone < listBox2.Items.Count; _listboxone++)
                {
                    _ObjExpList = new object();
                    if (!listBox2.GetSelected(_listboxone))
                    {
                        _ObjExpList = listBox2.GetItemText(listBox2.Items[_listboxone]);
                        _ObjExp.Add(_ObjExpList);
                    }
                }

                listBox1.DataSource = null;
                listBox1.Items.Clear();

                listBox2.DataSource = null;
                listBox2.Items.Clear();

                listBox2.DataSource = _ObjExp.OrderBy(r => r).ToList();
                listBox1.DataSource = _Objimp.OrderBy(p => p).ToList();

                chkSelectAll2.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
Share this answer
 
v3

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