using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Forms; using Mesnac.Controls.Base; using System.Drawing; using System.Data; namespace Mesnac.Controls.Default { [ToolboxBitmap(typeof(DataGridView))] public partial class MCDataGridView : DataGridView, IBaseControl { #region 字段定义 private bool _mcVisible = true; //保存可见性 private bool _mcEnabled = true; //保存可用性 private bool _isValid = true; //保存有效性 private bool _isEventValid = true; //保存事件有效性 private bool _displayAllColumn; private List _dgvColumn = new List(); private List _clickActionList = new List(); private List _doubleClickActionList = new List(); private List _selectionChangedActionList = new List(); //private DataTable dt = null; #endregion #region 构造方法 public MCDataGridView() { InitializeComponent(); this.MultiSelect = false; this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.RowHeadersVisible = false; this.AllowUserToAddRows = false; this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; this.RowTemplate.Resizable = DataGridViewTriState.False; this.ReadOnly = true; this.RowTemplate.Height = 20; //this.Controls.Add(this.comboBox1); } public MCDataGridView(IContainer container) { container.Add(this); InitializeComponent(); this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.ReadOnly = true; } #endregion #region 解决多线程访问时显示大红叉的问题 protected override void OnPaint(PaintEventArgs e) { try { base.OnPaint(e); } catch { Invalidate(); } } #endregion #region 属性定义 [Description("是否显示所有列"), Category("Behavior")] public bool DisplayAllColumn { get { return _displayAllColumn; } set { _displayAllColumn = value; } } [Description("显示列配置"), Category("Behavior")] public List DgvColumn { get { return _dgvColumn; } set { _dgvColumn = value; } } public ComboBox mcCombobox { get { return this.comboBox1; } } #endregion #region 方法定义 /// /// 显示列 /// protected void displayColumn() { try { lock (this) { this.AutoGenerateColumns = true; for (int i = 0; i < this.Columns.Count; i++) { this.Columns[i].Visible = true; this.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } if (!this._displayAllColumn) { int index = 1; for (int i = 0; i < this.Columns.Count; i++) { DataGridViewColumns dgcs = new DataGridViewColumns(); dgcs = this._dgvColumn.Where(t => t.FiledData.ToLower() == this.Columns[i].DataPropertyName.ToLower()).FirstOrDefault(); if (dgcs != null)//显示 { this.Columns[i].HeaderText = dgcs.DisplayName; //this.Columns[i].Width = dgcs.DgvColumnWidth; } else { this.Columns[i].Visible = false; this.Columns[i].DisplayIndex = this.Columns.Count - index; index++; } } int j = 0; foreach (DataGridViewColumns item in this._dgvColumn) { for (int i = 0; i < this.Columns.Count; i++) { if (this.Columns[i].DataPropertyName.ToLower() == item.FiledData.ToLower()) { if (j == this._dgvColumn.Count - 1) { this.Columns[i].DisplayIndex = this.Columns.Count - 1; } else { this.Columns[i].DisplayIndex = j; j++; } break; } } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } #endregion #region 事件定义 public List ClickActionList { get { return this._clickActionList; } set { this._clickActionList = value; } } public List DoubleClickActionList { get { return _doubleClickActionList; } set { _doubleClickActionList = value; } } public List SelectionChangedActionList { get { return _selectionChangedActionList; } set { _selectionChangedActionList = value; } } #endregion #region 接口成员实现 public string MCKey { get; set; } public object MCValue { get { return this.DataSource == null ? new DataTable("MCDataGridView") : (DataTable)this.DataSource; } set { ;} } public IBaseControl MCRoot { get; set; } [TypeConverter(typeof(DataSourceConverter))] [Description("数据连接"), Category("数据")] public string MCDataSourceID { get; set; } public MCDataSource MCDataSource { get; set; } [Description("是否为数据库控件"), Category("数据")] public bool IsDbControl { get; set; } [Description("初始化SQL"), Category("数据")] public string InitDataSource { get; set; } [Description("执行SQL"), Category("数据")] public string ActionDataSource { get; set; } [Description("绑定数据源"), Category("数据")] public object BindDataSource { get { return this.DataSource == null ? new DataTable("MCDataGridView") : (DataTable)this.DataSource; } set { try { lock (this) { this.DataSource = (DataTable)value == null ? new DataTable() : (DataTable)value; displayColumn(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } [Description("操作类型"), Category("数据")] public DbOptionTypes DbOptionType { get; set; } [Description("是否可见"), Category("行为")] public bool MCVisible { get { return this._mcVisible; } set { lock (this) { this._mcVisible = value == null ? true : value; if (this.Site.DesignMode) this.Visible = true; else this.Visible = this._mcVisible; } } } [Description("是否可用"), Category("行为")] public bool MCEnabled { get { return this._mcEnabled; } set { lock (this) { this._mcEnabled = value == null ? true : value; { if (this.Site.DesignMode) { this.Enabled = true; } else { this.Enabled = this._mcEnabled; } } } } } public bool IsValid { get { return _isValid; } set { _isValid = value; } } public bool IsEventValid { get { return this._isEventValid; } set { this._isEventValid = value; } } #endregion #region 事件处理 protected override void OnCellDoubleClick(DataGridViewCellEventArgs e) { base.OnCellDoubleClick(e); } protected override void OnCellClick(DataGridViewCellEventArgs e) { base.OnCellClick(e); this.EndEdit(); this.comboBox1.Visible = false; } #endregion #region 表头合并试运行代码 LQK 20180724 /* MCDataGridView exG = new Utility.exGridView(); List colNameCollection=new List(); for (int i = 0; i < 10; i++) { //"colDraw"+i.ToString()是columnName的属性值 colNameCollection.Add("colDraw" + i.ToString()); } exG.MergeHeader(sender, e, colNameCollection, "0-9中奖号码分布图"); */ #region 合并列时使用到的位置和大小属性 int cTop = 0;//被合并表头区域的顶部坐标 int cLeft = 0;//被合并表头区域的左边坐标 /// /// 被合并表头区域的宽 /// int cWidth = 0; int cHeight = 0;//。。。高 #endregion /// /// 判断是否已经将datagridview的表头变高了,只增高一次。 /// public static bool isEnLarged = false; /// /// 合并表头,用在dataGridView的CellPainting事件中。 /// /// 需要重绘的dataGridview /// CellPainting中的参数 ///列的集合(列必须是连续的,第一列放在最前面) /// 列合并后显示的文本 public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e, List colNameCollection, string headerText) { if (e.RowIndex == -1) { DataGridView dataGridView1 = sender as DataGridView; string colName = dataGridView1.Columns[e.ColumnIndex].Name; if (!isEnLarged) { //0.扩展表头高度为当前的2倍 dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2; isEnLarged = true; } if (colNameCollection.Contains(colName)) { #region 重绘列头 //1.计算colLen个列的区域 if (colNameCollection.IndexOf(colName) == 0) { cTop = e.CellBounds.Top; cLeft = e.CellBounds.Left; cWidth = e.CellBounds.Width; cHeight = e.CellBounds.Height / 2; foreach (string colNameItem in colNameCollection) { if (colNameItem.Equals(colName)) {//除去自己一个,加了之后colLen-1个列的宽 continue; } cWidth += dataGridView1.Columns[colNameItem].Width; } } Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight); //2.把区域设置为背景色,没有列的分线及任何文字。 using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { e.Graphics.FillRectangle(backColorBrush, cArea); } //3.绘制新列头的边框 using (Pen gridPen = new Pen(dataGridView1.GridColor)) { //3.1 上部边框 e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop); using (Pen hilightPen = new Pen(Color.WhiteSmoke)) { //3.2 顶部高光 e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1); //3.3 左部反光线 e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2); } //3.4 下部边框 e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1); //3.5 右部边框 e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2); } //4.写文本 if (colNameCollection.IndexOf(colName) == 0) {//不是第一列则不写文字。 int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints); int wHeadCell = cWidth; int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6; using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor)) { e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3)); } } //5 绘制子列背景 int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度 using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1)); } //5.1绘制子列的边框 using (Pen gridPen = new Pen(dataGridView1.GridColor)) { using (Pen hilightPen = new Pen(Color.WhiteSmoke)) { //5.2 左部反光线 e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight); } //5.3 下部边框 e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight); //5.4 右部边框 e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2); } //5.5 写子列的文本 int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints); int wCell = e.CellBounds.Width; int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标 using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor)) { e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight)); } #endregion e.Handled = true; } } } #endregion } }