Next[UP] and Previous[Down] Button in ListView

In This Example we can Traverse the listview item up and down or Next and Previous.
Lets Start Step by Step.

Step 1 : Creating a Window Application.

Step 2 : Drag and Drop 3 Controls in Default page from Toolbox
1- ListView (Name=”listView1″)
2- Button (Name=”btnUP”,Text=”UP”)
3- Button (Name=”btnDown”,Text=”DOWN”)
Step 3: double Click on Up and Down Button for Generate the Click Event

Step 4 :Create function for Fill the list View

private void BindListView()
{
string[] array = { “ListItem1”, “ListItem2”, “ListItem3”, “ListItem4”, “ListItem5”, “ListItem6” };
var items = listView1.Items;
foreach (var value in array)
{
items.Add(value);
}
}

Step 5: Do Code for Up and Down Button

// UP Button
 private void btnUP_Click(object sender, EventArgs e)
        {
            int index = listView1.SelectedItems[0].Index;

            if (index < listView1.Items.Count - 1)
            {
                listView1.Select();
                listView1.Items[index + 1].Selected = true;
                btnDown.Enabled = true;
                if ((index = listView1.SelectedItems[0].Index) == listView1.Items.Count - 1)
                    btnUP.Enabled = false;
            }
            else
                btnUP.Enabled = false;
        }
// Down Button
        private void btnDown_Click(object sender, EventArgs e)
        {
            int index = listView1.SelectedItems[0].Index;
            if (index > 0)
            {
                listView1.Select();
                listView1.Items[index - 1].Selected = true;
                btnUP.Enabled = true;
                if ((index = listView1.SelectedItems[0].Index) == 0)
                    btnDown.Enabled = false;
            }
            else
                btnDown.Enabled = false; 
        }

Step 6: For Assistance i am Coping all page Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BindListView();
            listView1.MultiSelect = false;
            listView1.Select();
            listView1.Items[0].Selected = true;  
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            
        }

        private void BindListView()
        {
            string[] array = { "ListItem1", "ListItem2", "ListItem3", "ListItem4", "ListItem5", "ListItem6" };
            var items = listView1.Items;
            foreach (var value in array)
            {
                items.Add(value);
            }
        }
        private void btnUP_Click(object sender, EventArgs e)
        {
            int index = listView1.SelectedItems[0].Index;

            if (index < listView1.Items.Count - 1)
            {
                listView1.Select();
                listView1.Items[index + 1].Selected = true;
                btnDown.Enabled = true;
                if ((index = listView1.SelectedItems[0].Index) == listView1.Items.Count - 1)
                    btnUP.Enabled = false;
            }
            else
                btnUP.Enabled = false;
        }
        private void btnDown_Click(object sender, EventArgs e)
        {
            int index = listView1.SelectedItems[0].Index;
            if (index > 0)
            {
                listView1.Select();
                listView1.Items[index - 1].Selected = true;
                btnUP.Enabled = true;
                if ((index = listView1.SelectedItems[0].Index) == 0)
                    btnDown.Enabled = false;
            }
            else
                btnDown.Enabled = false; 
        }


    }
}
Advertisements
This entry was posted in Asp.Net. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s