183 lines
6.8 KiB
C#
183 lines
6.8 KiB
C#
using DevComponents.DotNetBar.Controls;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Drawing.Design;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using static System.Windows.Forms.ComboBox;
|
|
|
|
namespace _29WxOrder.MyControl
|
|
{
|
|
public class MyDataGridView : DataGridViewX
|
|
{
|
|
public ToolTip toolTip = new ToolTip();
|
|
|
|
private bool isCustomSort = false;
|
|
[Browsable(true), Description("是否自定义排序"), Category("自定义分组")]
|
|
public bool IsCustomSort
|
|
{
|
|
get { return isCustomSort; }
|
|
set
|
|
{
|
|
isCustomSort = value;
|
|
foreach (DataGridViewColumn colunm in this.Columns)
|
|
{
|
|
if (isCustomSort)
|
|
colunm.SortMode = DataGridViewColumnSortMode.NotSortable;
|
|
else
|
|
colunm.SortMode = DataGridViewColumnSortMode.Automatic;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static ComboBox cBox = new ComboBox();
|
|
[Browsable(true),
|
|
Description("要自定义排序的列"),
|
|
Category("自定义分组"),
|
|
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
|
|
Localizable(true),
|
|
Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
|
|
MergableProperty(false)]
|
|
public ObjectCollection CustomSortColumns { get; set; } = new ObjectCollection(new ComboBox());
|
|
|
|
public delegate void CustomSortEvent(object sender, string sortName, string sortType = "desc");
|
|
public event CustomSortEvent CustomSorted;
|
|
|
|
public MyDataGridView()
|
|
{
|
|
this.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dgv_RowPostPaint); // 重绘
|
|
this.ShowCellToolTips = false;
|
|
|
|
toolTip.Draw += toolTip1_Draw;
|
|
|
|
this.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(244, 244, 244);//隔行变色
|
|
this.ColumnHeaderMouseClick += MyDataGridView_ColumnHeaderMouseClick;
|
|
}
|
|
|
|
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
|
|
{
|
|
e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
|
|
e.Graphics.DrawRectangle(Pens.Chocolate, new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1));
|
|
e.Graphics.DrawString(toolTip.ToolTipTitle + e.ToolTipText, e.Font, Brushes.Red, e.Bounds);
|
|
}
|
|
|
|
// 重绘
|
|
private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
|
|
{
|
|
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.RowHeadersWidth - 4, e.RowBounds.Height);
|
|
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.RowHeadersDefaultCellStyle.Font, rectangle,
|
|
this.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
|
|
}
|
|
|
|
DataGridViewColumn colSort = null;
|
|
Label lblSort = null;
|
|
private void MyDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
|
|
{
|
|
if (!this.isCustomSort)
|
|
return;
|
|
|
|
if (e.ColumnIndex < 0)
|
|
return;
|
|
|
|
if (CustomSortColumns.Count > 0 && !CustomSortColumns.Cast<object>().Select(l => l.ToString()).Contains(this.Columns[e.ColumnIndex].Name))
|
|
return;
|
|
|
|
if (lblSort == null)
|
|
{
|
|
lblSort = new Label() { ForeColor = Color.White, BackColor = Color.Transparent, AutoSize = true, };
|
|
this.Controls.Add(lblSort);
|
|
|
|
this.ColumnWidthChanged += (s, e1) => onColResize();
|
|
this.ColumnHeadersHeightChanged += (s, e1) => onColResize();
|
|
this.Scroll += (s, e1) => onColResize();
|
|
this.Resize += (s, e1) => onColResize();
|
|
this.Sorted += (s, e1) => onColResize();
|
|
this.RowHeadersWidthChanged += (s, e1) => onColResize();
|
|
this.ColumnStateChanged += (s, e1) => onColResize();
|
|
}
|
|
|
|
if (colSort != null && colSort != this.Columns[e.ColumnIndex])
|
|
{
|
|
lblSort.Text = "";
|
|
colSort = this.Columns[e.ColumnIndex];
|
|
}
|
|
colSort = this.Columns[e.ColumnIndex];
|
|
|
|
if (lblSort.Text == "▲")
|
|
{
|
|
lblSort.Text = "";
|
|
colSort = null;
|
|
}
|
|
else if (lblSort.Text == "▼")
|
|
{
|
|
lblSort.Text = "▲";
|
|
}
|
|
else
|
|
{
|
|
lblSort.Text = "▼";
|
|
}
|
|
|
|
CustomSorted?.Invoke(this, colSort?.Name, lblSort.Text == "▲" ? "asc" : lblSort.Text == "▼" ? "desc" : null);
|
|
onColResize();
|
|
}
|
|
|
|
void onColResize()
|
|
{
|
|
if (colSort == null)
|
|
return;
|
|
|
|
int colLeft = (this.RowHeadersVisible ? this.RowHeadersWidth : 0) - (colSort.Frozen ? 0 : this.HorizontalScrollingOffset);
|
|
foreach (DataGridViewColumn col in this.Columns)
|
|
{
|
|
if (col.DisplayIndex >= colSort.DisplayIndex) break;
|
|
if (!col.Visible) continue;
|
|
colLeft += col.Width;
|
|
}
|
|
|
|
var newPoint = new Point(colLeft + 1, (this.ColumnHeadersHeight + 1) / 2 - lblSort.Height / 2);
|
|
newPoint.X += colSort.Width - lblSort.Width - 2;
|
|
var minLeft = colLeft;
|
|
var maxLeft = colLeft + colSort.Width - lblSort.Width;
|
|
|
|
if (newPoint.X < minLeft) newPoint.X = minLeft;
|
|
if (newPoint.X > maxLeft) newPoint.X = maxLeft;
|
|
|
|
lblSort.Location = newPoint;
|
|
}
|
|
|
|
#region 鼠标浮动
|
|
protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
|
|
{
|
|
base.OnCellMouseMove(e);
|
|
|
|
if (e.RowIndex >= 0)
|
|
{
|
|
this.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
|
|
}
|
|
}
|
|
|
|
protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
|
|
{
|
|
base.OnCellMouseLeave(e);
|
|
|
|
if (e.RowIndex != -1)
|
|
{
|
|
if (e.RowIndex % 2 == 0)
|
|
{
|
|
this.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
|
|
}
|
|
else
|
|
{
|
|
this.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(244, 244, 244);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|