ASP.NET - how to swap value between two listview

Asked By msakt on 05-Jun-12 12:04 AM
how to swap value between two listview
DL M replied to msakt on 05-Jun-12 12:12 AM
Hi..

Good morning..

example here about swap value between two listview

 private void MoveListViewItem(ref ListView lv,bool moveUp)
    {
    string cache;
    int selIdx;

    selIdx = lv.SelectedItems[0].Index;
    if(moveUp)
    {
      // ignore moveup of row(0)
      if(selIdx==0)
      return;
      
      // move the subitems for the previous row
      // to cache to make room for the selected row
      for(int i=0; i < lv.Items[selIdx].SubItems.Count; i++)
      {
      cache = lv.Items[selIdx - 1].SubItems[i].Text;
      lv.Items[selIdx - 1].SubItems[i].Text =
        lv.Items[selIdx].SubItems[i].Text;
      lv.Items[selIdx].SubItems[i].Text = cache;
      }
      lv.Items[selIdx-1].Selected=true;
      lv.Refresh();
      lv.Focus();
    }
    else
    {
      // ignore movedown of last item
      if(selIdx==lv.Items.Count-1)
      return;
      // move the subitems for the next row
      // to cache so we can move the selected row down
      for(int i = 0; i < lv.Items[selIdx].SubItems.Count; i++)
      {
      cache = lv.Items[selIdx + 1].SubItems[i].Text;
      lv.Items[selIdx + 1].SubItems[i].Text =
        lv.Items[selIdx].SubItems[i].Text;
      lv.Items[selIdx].SubItems[i].Text = cache;
      }
      lv.Items[selIdx+1].Selected=true;
      lv.Refresh();
      lv.Focus();
    }
    }
Jitendra Faye replied to msakt on 05-Jun-12 12:22 AM
Refer this article -

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

Here you will get code.
dotnet freak replied to msakt on 05-Jun-12 12:42 AM
Follow this example for  this:
http://www.vbdotnetheaven.com/uploadfile/ahsanshakir/how-to-swap-two-rows-of-listview-in-upwarddownward-direction/
Neha Garg replied to msakt on 05-Jun-12 12:53 AM
Hi...

See the complete solution:

aspx Code:
<div>
    <asp:ListView ID="ListView1" runat="server" OnSelectedIndexChanging="ListView1_SelectedIndexChanging">
      <ItemTemplate>
        <tr>
          <td>
            <asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
          </td>
          <td>
            <%# Eval("test")%>
          </td>
          <br />
        </tr>
      </ItemTemplate>
      <SelectedItemTemplate>
        <tr style="background-color: #FFFF66; color: #808000;">
          <td>
            <asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
              ForeColor="Red" />
          </td>
          <td style="background-color: #FFFF66; color: #808000;">
            <%# Eval("test")%>
          </td>
          <br />
        </tr>
      </SelectedItemTemplate>
    </asp:ListView>
    <br />
    <asp:Button ID="Button6" runat="server" Text="Move" OnClick="Button1_Click" Style="height: 26px" />
    <asp:Label ID="lblStatus" runat="server"></asp:Label>
    <br />
    <asp:ListView ID="ListView2" runat="server">
      <ItemTemplate>
        <tr style="background-color: #00FF00">
          <td>
            <%# Eval("test")%>
          </td>
          <br />
        </tr>
      </ItemTemplate>
    </asp:ListView>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button7" runat="server" Text="Save" OnClick="Button2_Click" />
  </div>


Code behind:

DataTable Datat = new DataTable();
DataTable Data2 = new DataTable();
protected void Button2_Click(object sender, EventArgs e)
{
  if (TextBox1.Text == string.Empty)
  {
    lblStatus.Text = "Enter value ";
    return;
  }
  if (ViewState["Datat"] == null)
  {
    DataColumn col = new DataColumn("test");
    col.DataType = System.Type.GetType("System.String");
    Datat.Columns.Add(col);
  }
  else
  {
    Datat = (DataTable)ViewState["Datat"];
  }
 
  DataRow dr = Datat.NewRow();
  dr[0] = TextBox1.Text; ;
 
  Datat.Rows.Add(dr);
  ListView1.DataSource = Datat;
  ListView1.DataBind();
  ViewState.Add("Datat", Datat);
  lblStatus.Text = "your Item saved ";
  TextBox1.Text = string.Empty;
}
 
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
  ListView1.SelectedIndex = e.NewSelectedIndex;
  lblStatus.Text = "your Item selected ";
 
  Datat = (DataTable)ViewState["Datat"];
  ListView1.DataSource = Datat;
}
protected void Button1_Click(object sender, EventArgs e)
{
  Datat = (DataTable)ViewState["Datat"];
  if (ViewState["Data2"] == null)
  {
    DataColumn col = new DataColumn("test");
    col.DataType = System.Type.GetType("System.String");
    Data2.Columns.Add(col);
  }
  else
  {
    Data2 = (DataTable)ViewState["Data2"];
  }
  string ItemValue = Datat.Rows[ListView1.SelectedIndex][0].ToString();
  Datat.Rows.RemoveAt(ListView1.SelectedIndex);
  DataRow dr2 = Data2.NewRow();
  dr2[0] = ItemValue;
  Data2.Rows.Add(dr2);
  ListView2.DataSource = Data2;
  ListView2.DataBind();
  ViewState.Add("Data2", Data2);
  ListView1.DataSource = Datat;
  ListView1.DataBind();
  ViewState.Add("Datat", Datat);
  lblStatus.Text = "your Item Moved ";
}





TSN ... replied to DL M on 05-Jun-12 01:04 AM

IF your Trying for Copying the selected items to another ListView you can try in this way.

private void ListView1_SelectedIndexChanged_UsingItems(oject sender, System.EventArgs e)

     {

         ListView.SelectedListViewItemCollection lstCoolection =

              this.ListView1.SelectedItems;

        

         foreach ( ListViewItem item in lstCoolection)

         {

              ListView2.Items.Add((ListViewItem)item.Clone());

         }

         // Output the price to TextBox1.

              }

Chintan Vaghela replied to msakt on 05-Jun-12 01:08 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