0

I have this two list-box In that first list-box is fill on Combo-box Selected index changed, so List-box 1 is Bounded. Now when I press the > button all selected item in List-box 1 is display in List-box 2.

But instead of Names, I get System.Data.DataRowView so my question is I want Names instead of this System.Data.DataRowView my code is this

private void btnSelect1ItemFrom_Click(object sender, EventArgs e)
    {
        if (listBoxSelectToLedger.Items.Count > 0)
        {
            for (int i = 0; listBoxSelectToLedger.Items.Count > i; )
            {
                listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
            }              
        }
        if (listBoxSelectFromLedger.SelectedItem != null)
        {
          **  for (int i = 0; i < listBoxSelectFromLedger.SelectedItems.Count; i++)
            {
                listBoxSelectToLedger.Items.Add(listBoxSelectFromLedger.SelectedItems[i].ToString());
            }   **           
        }
        else
        {
            MessageBox.Show("No item Selected");
        }

* I think I am some where Wrong in Second IF Condition in my Code * enter image description here

Plz Help me Thanks in Advance

Deep Patel
  • 45
  • 1
  • 8

3 Answers3

0

Try this...

  private void button1_Click(object sender, EventArgs e)
    {
        if(listBoxFrom.SelectedItems.Count>0)
        {
          for (int x = listBoxFrom.SelectedIndices.Count - 1; x >= 0; x--)
            {
                int idx = listBoxFrom.SelectedIndices[x];
                listBoxTo.Items.Add(listBoxFrom.Items[idx]);
                listBoxFrom.Items.RemoveAt(idx);
            } 

        }
    }
user3502966
  • 59
  • 2
  • 14
0

Hiii.. Deep, use the below code to add ListItem.

 foreach (ListItem LI in listBoxFrom.Items)
        {
            if (LI.Selected)
                listBoxTo.Items.Add(LI);
        }

To add in to 2nd listbox and remove that from the first listbox you can use below code:

        int[] indices = listBoxFrom.GetSelectedIndices();
        for (int i = indices.Length - 1; i >= 0; i--)
        {
            ListItem LI = listBoxFrom.Items[indices[i]];
            listBoxTo.Items.Add(LI);
            listBoxFrom.Items.RemoveAt(indices[i]);
        }

put your No items selected message where you require.

Girish Vadhel
  • 735
  • 1
  • 5
  • 17
0

I got answer of my own question. i have to set my DataRowView object

if (listBoxSelectToLedger.Items.Count > 0)
        {
            for (int i = 0; listBoxSelectToLedger.Items.Count > i; i = 0)
            {
                listBoxSelectToLedger.Items.Remove(listBoxSelectToLedger.Items[i].ToString());
            }
        }
        if (listBoxSelectFromLedger.SelectedItem != null)
        {

            foreach (DataRowView objDataRowView in listBoxSelectFromLedger.SelectedItems)
            {
                listBoxSelectToLedger.Items.Add(objDataRowView["item_name"].ToString());
            }
        }
        else
        {
            MessageBox.Show("No Item selected");

        }
Deep Patel
  • 45
  • 1
  • 8