Thursday, July 29, 2010

Display Values in the List view and Treeview control


Write a program to display values in the list view and treeview control and to perform grouping, sorting and list View Modes functionalities

Data Base Code:
create table customer
(
 id int,
 sName varchar(234),
 Phone varchar(234),
 EmailId varchar(234),
 groups varchar(234)
)
insert into customer values(1,'srikanth','7145641545','srikanth@rocketmail.com','Friends')
select * from customer



using System;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;

namespace listview
{
    public partial class Form1 : Form
    {
        SqlConnection cnn = new SqlConnection("initial Catalog =master; Data Source =.; Integrated Security = true;MultipleActiveResultSets  = true;");
        ArrayList list = new ArrayList();
        string[] values = new string[4]; string groups;
        TreeNode node;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
            listView1.View = View.Details;
            listView1.Columns.Add("ID", 150);
            listView1.Columns.Add("Name", 150);
            listView1.Columns.Add("Phone", 150);
            listView1.Columns.Add("EmailID", 150);
            listView1.FullRowSelect = true;
            SqlCommand cmd = new SqlCommand("select distinct groups from customer", cnn);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                groups = dr.GetString(0);
                node = treeView1.Nodes.Add(groups);
                listView1.Groups.Add(new ListViewGroup(groups,groups));
                SqlCommand cdm = new SqlCommand("select * from customer where groups ='"+groups+"'", cnn);
                SqlDataReader d = cdm.ExecuteReader();
                while (d.Read())
                {
                    values[0] = d.GetInt32(0).ToString();
                    values[1] = d.GetString(1);
                    values[2] = d.GetString(2);
                    values[3] = d.GetString(3);
                    ListViewItem itm = new ListViewItem(values);
                    listView1.Items.Add(itm);
                    listView1.Groups[groups].Items.Add(itm);
                    node.Nodes.Add(values[1]);
                }
                d.Close();
            }
            dr.Close();
           
        }

        private void thumbnailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void tilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }

        private void iconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Tile;
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Details;
        }

        private void showIngroupsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (showIngroupsToolStripMenuItem.Checked == true)
            {
                listView1.ShowGroups = true;
            }
            else
            {
                listView1.ShowGroups = false;
            }
        }


       

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            listView1.Items.Clear();
            SqlCommand cdm = new SqlCommand("select * from customer where groups ='" + treeView1.SelectedNode.Text + "'", cnn);
            SqlDataReader d = cdm.ExecuteReader();
            groups = treeView1.SelectedNode.Text;
            listView1.Groups.Add(new ListViewGroup(groups, groups));
            while (d.Read())
            {
                values[0] = d.GetInt32(0).ToString();
                values[1] = d.GetString(1);
                values[2] = d.GetString(2);
                values[3] = d.GetString(3);
                ListViewItem itm = new ListViewItem(values);
                listView1.Items.Add(itm);
                listView1.Groups[groups].Items.Add(itm);
            }
            d.Close();
        }

     

       

      
    }
}

No comments:

Post a Comment