.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(); } } }}
- .net cf 使用listview 显示表格复选框
- 使用复选框控制表格中的文本框
- 使用django form生成复选框表格
- listview与复选框的合并使用
- jquery完成带复选框的表格行高亮显示
- 使用复选框添加显示隐藏层
- Android使用popwindow弹窗显示listview表格
- asp.net使用listview分页显示数据
- asp.net使用listview分页显示数据
- JQuery实现表格复选框
- TcxTreeList显示复选框
- jquery实现带复选框的表格行选中删除时高亮显示
- JQuery各种选择器并实现定时显示隐藏、表格奇偶换色、复选框全选全不选、
- ListView的图片复选框
- listview增加一个复选框
- android ListView的复选框
- Listview复选框的选择
- Bootstrap之表格checkbox复选框全选(http://blog.csdn.net/shangmingchao)
- CentOS中文显示方格问题解决
- 自我理解字符串变量和其他变量使用的不同
- static用法小结
- 创建多进程的函数
- HDU/HDOJ 1573 X问题 非互质情况下的中国剩余定理
- .net cf 使用listview 显示表格复选框
- PHP——Dreamweaver新建站点
- EOF
- 内存转换和分段 Memory Translation and Segmentation
- Android游戏开发之Tween动画的实现(三十二)
- SqlParameter.Size 属性
- 如何查看端口号被哪个程序占用
- 第十二章 5 IO包下的其他对象
- emacs 笔记 常用功能 配置