ASP.NET - how move value one listview to another listview

Asked By msakt on 04-Jun-12 09:07 AM
have two list view one listview have
TSN ... replied to msakt on 04-Jun-12 09:09 AM

For moving the items from one list view to another try this below..

private static void MoveSelectedItems(ListView source, ListView target) 
{   
  while (source.SelectedItems.Count > 0) 
  { 
    ListViewItem temp = source.SelectedItems[0]; 
    source.Items.Remove(temp); 
    target.Items.Add(temp); 
  }       
} 
Ravi Maurya replied to msakt on 04-Jun-12 09:13 AM
There are a couple of different ways.

If you want to copy the items from a to b:

private static void CopySelectedItems(ListView source, ListView target)
{
  foreach (ListViewItem item in source.SelectedItems)
  {
    target.Items.Add((ListViewItem)item.Clone());
  }
}

If you want to move the items from a to b:

private static void MoveSelectedItems(ListView source, ListView target)
{   
  while (source.SelectedItems.Count > 0)
  {
    ListViewItem temp = source.SelectedItems[0];
    source.Items.Remove(temp);
    target.Items.Add(temp);
  }       
}

hope this might help you
msakt replied to Ravi Maurya on 04-Jun-12 09:17 AM
where i call this method and how pass source and destination
Ravi Maurya replied to msakt on 04-Jun-12 09:21 AM
you can call these methods on any event say click event of Button 

and pass the source control id with target control id 

for eg. CopySelectedItems(lvsource, lvtarget);
Try this and let me know
Jitendra Faye replied to msakt on 04-Jun-12 09:21 AM
Refer this article -

http://www.codeproject.com/Questions/356189/transfer-data-from-listview-to-another-listview

Here you will get code.
msakt replied to Ravi Maurya on 04-Jun-12 09:41 AM
error occure   does not contain defenition for selecteditem and no extension method for selecteditems same for clone
msakt replied to msakt on 04-Jun-12 09:44 AM
error occure    does not contain defenition for selecteditems and no extension method for selecteditems its same for clone
DL M replied to msakt on 04-Jun-12 01:38 PM
Hi..

You can also drag & drop item from one listview to another listview & another to one.

show below code sample
------------------

private void listView1_DragDrop(object sender,System.Windows.Forms.DragEventArgs e)
{
string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
string[] items = textBox1.Split(',');
listView1.Items.Add(new ListViewItem(items,0));
lv1_mdown = false ;
lv2_mdown = false ;
}

private void listView2_DragDrop(object sender,System.Windows.Forms.DragEventArgs e)
{
string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
string[] items = textBox1.Split(',');
listView2.Items.Add(new ListViewItem(items,0));
lv2_mdown = false ;
lv1_mdown = false ;
}

read complete article here
http://www.c-sharpcorner.com/UploadFile/skulkarni/ImlementingDragandDropinListViewControls11252005035642AM/ImlementingDragandDropinListViewControls.aspx

You can also use WPF, show below link
http://geekswithblogs.net/sonam/archive/2009/03/02/listview-dragdrop-in-wpfmultiselect.aspx

kalpana aparnathi replied to msakt on 04-Jun-12 02:54 PM
hi,

Try below code:

protected void ListView1_OnPreRender(object sender, EventArgs e)
{
    if (ListView1.EditIndex > -1)
      {
      // for avoid error
        }
      else
      {
        TextBox tb = null;


        foreach (ListViewDataItem item in ListView1.Items) {
            tb = (TextBox)item.FindControl("txtInsideId");

            if (tb.Text != null) {
                txtOutsideId.Text = tb.Text.ToString;
            }
        }

    }

}


Regards,
Chintan Vaghela replied to msakt on 05-Jun-12 01:07 AM

Hi Frndz,

Move list view selected Item to other list view then try to use following code

private static void MoveSelectedItems(ListView source, ListView target)

    {

      while (source.Items.Count > 0)

      {

        ListViewDataItem temp = source.Items[0];

        source.Items.Remove(temp);

        target.Items.Add(temp);

      }

    }

 

Hope this helpful!

Thanks

 

 

 

 

Ravi Maurya replied to msakt on 05-Jun-12 03:26 AM
hi,

Sorry for late reply, try this example done only you

Source:

<asp:ListBox ID="lbSource" runat="server" SelectionMode="Multiple">
      <asp:ListItem Value="s1">S1</asp:ListItem>
      <asp:ListItem Value="s2">S2</asp:ListItem>
      <asp:ListItem Value="s3">s3</asp:ListItem>
      <asp:ListItem Value="s4">s4</asp:ListItem>
      <asp:ListItem Value="s5">s5</asp:ListItem>
    </asp:ListBox>
    <asp:ListBox ID="lbDestination" runat="server" SelectionMode="Multiple">
      <asp:ListItem Value="d1">D1</asp:ListItem>
      <asp:ListItem Value="d2">D2</asp:ListItem>
    </asp:ListBox>
 
<asp:Button ID="btnCopy" runat="server" Text="Copy" onclick="btnCopy_Click" />
 
  <asp:Button ID="btnMove" runat="server" Text="move" onclick="btnMove_Click" /> Code Behind:
protected void btnCopy_Click(object sender, EventArgs e)
  {
    for (int i = lbSource.Items.Count - 1; i >= 0; i--)
    {
      if (lbSource.Items[i].Selected == true)
      {
        lbDestination.Items.Add(lbSource.Items[i]);
      }
    }
  }
  protected void btnMove_Click(object sender, EventArgs e)
  {
    for (int i = lbSource.Items.Count - 1; i >= 0; i--)
    {
      if (lbSource.Items[i].Selected == true)
      {
        lbDestination.Items.Add(lbSource.Items[i]);
        ListItem li = lbSource.Items[i];
        lbSource.Items.Remove(li);
      }
    }
  }
Try this and let me know if you face any problem.