Friday, May 31, 2013

Show gridlines in Listview control on Compact framework


Below is the sample code to demonstrate the Grid lines in ListView Control on compact framework.

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SmartDeviceProject2
{
    public partial class ListViewControl : Form
    {
        DataTable dtEmployees = new DataTable("EmpList");
        private const uint LVM_FIRST = 0x1000;
        private const uint LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;
        private const uint LVM_GETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 55;
        private const uint LVS_EX_GRIDLINES = 0x00000001;
        [DllImport("coredll.dll")]
        private static extern uint SendMessage(IntPtr hwnd, uint msg, uint wparam, uint lparam);
        public ListViewControl()
        {
            InitializeComponent();
        }
        private void createEmpTable()
        {
            dtEmployees.Columns.Add("S", typeof(Boolean));
            dtEmployees.Columns.Add("EmpNo", typeof(String));
            dtEmployees.Columns.Add("EmpName", typeof(String));
        }
        private void ListViewControl_Load(object sender, EventArgs e)
        {
            createEmpTable();
        }
        private void BindListView()
        {
            DataRow dr = dtEmployees.NewRow();
            dr["S"] = true;
            dr["EmpNo"] = "1";
            dr["EmpName"] = "Shreekanth Gaanji";
            dtEmployees.Rows.Add(dr);
            dr = dtEmployees.NewRow();
            dr["S"] = false;
            dr["EmpNo"] = "2";
            dr["EmpName"] = "Ashwini Gaanji";
            dtEmployees.Rows.Add(dr);
            dr = dtEmployees.NewRow();
            dr["S"] = false;
            dr["EmpNo"] = "3";
            dr["EmpName"] = "Manjunath Gaanji";
            dtEmployees.Rows.Add(dr);
            ColumnHeader colHeader_S = new ColumnHeader();
            ColumnHeader colHeader_EmpNo = new ColumnHeader();
            ColumnHeader colHeader_EmpName = new ColumnHeader();
            colHeader_S.Text = "S";
            colHeader_S.TextAlign = HorizontalAlignment.Left;
            colHeader_S.Width = (ClientSize.Width * 10) / 100;
            colHeader_EmpNo.Text = "EmpNo";
            colHeader_EmpNo.TextAlign = HorizontalAlignment.Left;
            colHeader_EmpNo.Width = (ClientSize.Width * 40) / 100;
            colHeader_EmpName.Text = "EmpName";
            colHeader_EmpName.TextAlign = HorizontalAlignment.Left;
            lstEmployees.View = View.Details;
            lstEmployees.FullRowSelect = true;
            lstEmployees.CheckBoxes = true;
            lstEmployees.Columns.Add(colHeader_S);
            lstEmployees.Columns.Add(colHeader_EmpNo);
            lstEmployees.Columns.Add(colHeader_EmpName);
            ListViewItem itemGroupName;
            for (int i = 0; i < dtEmployees.Rows.Count; i++)
            {
                itemGroupName = new ListViewItem();
                itemGroupName.Checked = Convert.ToBoolean(dtEmployees.Rows[i]["S"]);
                itemGroupName.SubItems.Add(Convert.ToString(dtEmployees.Rows[i]["EmpNo"]));
                itemGroupName.SubItems.Add(Convert.ToString(dtEmployees.Rows[i]["EmpName"]));
                lstEmployees.Items.Add(itemGroupName);
            }
            EnableGridlines(lstEmployees);
        }
        public void EnableGridlines(ListView listView)
        {
            var style = SendMessage(
                    listView.Handle,
                    LVM_GETEXTENDEDLISTVIEWSTYLE,
                    0,
                    0);
            style |= LVS_EX_GRIDLINES;
            style = SendMessage(
                   listView.Handle,
                   LVM_SETEXTENDEDLISTVIEWSTYLE,
                   0,
                   style);
        }
    }
}


Here is the OutPut.

 

1 comment:

  1. Hello Shreekanth,

    I want move checkbox from first column to last column in ListView , help me please.

    Thank you.

    ReplyDelete