.net cf 使用listview 显示表格复选框

来源:互联网 发布:json转化为list 编辑:程序博客网 时间:2024/05/16 15:11

  在.net cf里,datagrid没有checkbox,但是项目中又确实需要,在网上查了之后,只有使用listview这种方法了,就特意做了一个listview的datagrid形式的表格出来,封装成一个控件使用。控件在显示时,自动按照显示的最长内容的列宽显示。

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;using ZLDrivers;namespace LZLControls{    public partial class ZLListViewGrid : ListView, ISupportInitialize    {        public ZLListViewGrid()        {            //设置listView的显示属性            this.View = View.Details;            this.FullRowSelect = true;            this.HeaderStyle = ColumnHeaderStyle.Nonclickable;            InitializeComponent();        }        #region 设计组件 ISupportInitialize Members        void ISupportInitialize.BeginInit()        {        }        void ISupportInitialize.EndInit()        {        }        #endregion        #region 增加的属性        private const uint LVM_FIRST = 0x1000;        private const uint LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;        private const uint LVM_GETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 55;        private void SetStyle(uint style, bool enable)        {            uint currentStyle = Win32.SendMessage(              Handle,              LVM_GETEXTENDEDLISTVIEWSTYLE,              0,              0);            if (enable)                Win32.SendMessage(                  Handle,                  LVM_SETEXTENDEDLISTVIEWSTYLE,                  0,                  currentStyle | style);            else                Win32.SendMessage(                  Handle,                  LVM_SETEXTENDEDLISTVIEWSTYLE,                  0,                  currentStyle & ~style);        }        private const uint LVS_EX_GRIDLINES = 0x00000001;        private bool gridLines = true;        public bool GridLines        {            get { return gridLines; }            set            {                gridLines = value;                SetStyle(LVS_EX_GRIDLINES, gridLines);            }        }        private const uint LVS_EX_DOUBLEBUFFER = 0x00010000;        private bool doubleBuffering = true;        public bool DoubleBuffering        {            get { return doubleBuffering; }            set            {                doubleBuffering = value;                SetStyle(LVS_EX_DOUBLEBUFFER, doubleBuffering);            }        }        private const uint LVS_EX_GRADIENT = 0x20000000;        private bool gradient = false;        public bool Gradient        {            get { return doubleBuffering; }            set            {                gradient = value;                SetStyle(LVS_EX_GRADIENT, gradient);            }        }        #endregion        public int CurrentRowIndex        {            get            {                if (this.FocusedItem == null)                {                    return -1;                }                else                {                    return this.FocusedItem.Index;                }            }            set            {                if ( this.Items.Count>0 )                {                    if (value < 0)                    {                        if ( CurrentRowIndex>=0 )                        {                            this.Items[CurrentRowIndex].Selected = false;                        }                    }                    else if ( value<this.Items.Count )                    {                        this.Items[value].Selected = true;                        this.Items[value].Focused = true;                    }                    else                    {                        throw new Exception("序号不正确!");                    }                }                else                {                    throw new Exception("序号不正确!");                }                            }        }        private void InsertDetail(ref DataTable dt)        {            for (int index = 0; index < dt.Rows.Count; index++)            {                ListViewItem item = new ListViewItem();                item.SubItems[0].Text = dt.Rows[index][0].ToString();   // 这必须给0赋值                for (int j = 1; j < dt.Columns.Count; j++)                {                    item.SubItems.Add(dt.Rows[index][j].ToString());                }                this.Items.Add(item);            }        }        public DataTable DataSource        {            set            {                if (value != null)                {                    this.BeginUpdate();                    AutoAdjushColumnWidth(ref value);                    InsertDetail(ref value);                    this.EndUpdate();                }                else                {                    this.Clear();                }            }        }        /// <summary>        /// 自动调整列宽        /// </summary>        public void AutoAdjushColumnWidth( ref DataTable dt )        {            //if (DataSource != null)            {                //DataTable dt = (DataTable)DataSource;                Graphics g = this.Parent.CreateGraphics();                int rowsCount = dt.Rows.Count;                int[] szMaxWidth = new int[dt.Columns.Count];                SizeF textSize;                int i, j, nExtendWidth;                /// 空出一个字符画表格的空间                nExtendWidth = (int)g.MeasureString("a", this.Font).Width * 2;      // listview与datagrid不一样,范围更大一些                /// 设置标题列宽                for (i = 0; i < szMaxWidth.Length; i++)                {                    szMaxWidth[i] = (int)g.MeasureString(dt.Columns[i].Caption, this.Font).Width + nExtendWidth;                }                /// 获取最大列宽                for (i = 0; i < rowsCount; i++)                {                    for (j = 0; j < szMaxWidth.Length; j++)                    {                        textSize = g.MeasureString(dt.Rows[i][j].ToString(), this.Font);                        if (textSize.Width > szMaxWidth[j])                        {                            szMaxWidth[j] = (int)textSize.Width;                        }                    }                }                if ( CheckBoxes && szMaxWidth.Length>0 )                {                    szMaxWidth[0] += nExtendWidth * 1;                }                /// 添加字段名                this.Clear();                for (i = 0; i < szMaxWidth.Length; i++)                {                    this.Columns.Add(dt.Columns[i].ColumnName, szMaxWidth[i] + nExtendWidth, HorizontalAlignment.Left);                }                g.Dispose();            }        }    }}


 

原创粉丝点击