2020-05-11 21:11:29 +08:00
|
|
|
|
/******************************************************************************
|
|
|
|
|
* SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。
|
2023-01-07 18:12:33 +08:00
|
|
|
|
* CopyRight (C) 2012-2023 ShenYongHua(沈永华).
|
2021-02-20 15:45:47 +08:00
|
|
|
|
* QQ群:56829229 QQ:17612584 EMail:SunnyUI@QQ.Com
|
2020-05-11 21:11:29 +08:00
|
|
|
|
*
|
|
|
|
|
* Blog: https://www.cnblogs.com/yhuse
|
|
|
|
|
* Gitee: https://gitee.com/yhuse/SunnyUI
|
|
|
|
|
* GitHub: https://github.com/yhuse/SunnyUI
|
|
|
|
|
*
|
|
|
|
|
* SunnyUI can be used for free under the GPL-3.0 license.
|
|
|
|
|
* If you use this code, please keep this note.
|
|
|
|
|
* 如果您使用此代码,请保留此说明。
|
|
|
|
|
******************************************************************************
|
2022-01-21 09:52:05 +08:00
|
|
|
|
* 文件名称: UIDataGridView.cs
|
2020-05-11 21:11:29 +08:00
|
|
|
|
* 文件说明: 表格
|
2022-01-05 21:57:47 +08:00
|
|
|
|
* 当前版本: V3.1
|
2020-05-11 21:11:29 +08:00
|
|
|
|
* 创建日期: 2020-01-01
|
|
|
|
|
*
|
|
|
|
|
* 2020-01-01: V2.2.0 增加文件说明
|
2020-07-15 21:26:07 +08:00
|
|
|
|
* 2020-04-25: V2.2.4 功能增强、美化
|
|
|
|
|
* 2020-07-15: V2.2.6 更改默认配置为原生
|
2020-07-30 21:21:47 +08:00
|
|
|
|
* 2020-07-18: V2.2.6 重绘水平滚动条
|
2021-08-17 16:55:01 +08:00
|
|
|
|
* 2020-08-22: V2.2.7 更新了水平和垂直滚动条的显示,优化滚动效果
|
2020-09-17 21:20:47 +08:00
|
|
|
|
* 2020-08-28: V2.2.7 调整水平滚动条
|
2021-08-17 16:55:01 +08:00
|
|
|
|
* 2021-03-25: V3.0.2 修改垂直滚动条和原版一致,并增加翻页方式滚动
|
2021-04-01 21:56:09 +08:00
|
|
|
|
* 2021-04-01: V3.0.2 编辑输入时,用Enter键代替Tab键跳到下一个单元格
|
2021-08-17 16:55:01 +08:00
|
|
|
|
* 2021-04-29: V3.0.3 设置数据行头部颜色
|
|
|
|
|
* 2021-05-22: V3.0.4 增加了一个RowHeight,默认23
|
|
|
|
|
* 2021-06-27: V3.0.4 自定义单元格颜色
|
2022-01-21 09:52:05 +08:00
|
|
|
|
* 2022-01-21: V3.1.0 更新单选时选中值SelectedIndex值
|
2022-04-16 21:19:31 +08:00
|
|
|
|
* 2022-04-16: V3.1.3 增加滚动条的颜色设置
|
2022-04-26 14:44:23 +08:00
|
|
|
|
* 2022-04-26: V3.1.8 解决原生控件DataSource绑定List,并且List为空,出现”索引-1没有值“错误
|
2022-06-10 17:59:38 +08:00
|
|
|
|
* 2022-06-10: V3.1.9 不再判断DataSource绑定List为空,出现”索引-1没有值“用户自行判断
|
2022-06-11 12:54:26 +08:00
|
|
|
|
* 2022-06-11: V3.1.9 隐藏 ShowRect, 设置原生属性:
|
|
|
|
|
* BorderStyle = BorderStyle.FixedSingle;
|
|
|
|
|
* 2022-06-11: V3.1.9 隐藏 ShowGridLine, 设置原生属性:
|
|
|
|
|
* CellBorderStyle = DataGridViewCellBorderStyle.Single;
|
|
|
|
|
* 2022-06-11: V3.1.9 隐藏 RowHeight, 用 SetRowHeight() 代替,或设置原生属性:
|
|
|
|
|
* AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
|
|
|
|
|
* RowTemplate.Height 设置为高度
|
2022-06-22 21:33:50 +08:00
|
|
|
|
* 2022-06-22: V3.2.0 删除 ShowRect、ShowGridLine、RowHeight三个属性
|
2022-07-11 17:48:52 +08:00
|
|
|
|
* 2022-07-11: V3.2.1 修复一处滚动条的显示位置
|
2022-07-11 23:22:32 +08:00
|
|
|
|
* 2022-07-11: V3.2.1 增加滚动条边框线的设置
|
2022-07-29 10:21:21 +08:00
|
|
|
|
* 2022-07-28: V3.2.2 修复了ScrollBars为None时仍然显示滚动条的问题
|
|
|
|
|
* 2022-07-28: V3.2.2 修复了单行时表格高度低时,垂直滚动条拖拽至底部出错的问题
|
2022-10-14 16:44:38 +08:00
|
|
|
|
* 2022-10-14: V3.2.6 增加了可设置垂直滚动条宽度的属性
|
2023-06-28 17:32:50 +08:00
|
|
|
|
* 2023-06-28: V3.3.9 增加了可设置水平滚动条宽度的属性,但可能会遮挡最下面数据行的数据,看情况使用
|
2023-07-12 10:01:33 +08:00
|
|
|
|
* 2023-07-12: V3.4.0 修复了有冻结行时垂直滚动条点击时出错的问题
|
2020-05-11 21:11:29 +08:00
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-06-27 23:07:30 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Sunny.UI
|
|
|
|
|
{
|
2022-04-11 13:18:21 +08:00
|
|
|
|
public class UIDataGridView : DataGridView, IStyleInterface, IZoomScale
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2020-07-15 21:26:07 +08:00
|
|
|
|
private readonly UIScrollBar VBar = new UIScrollBar();
|
2020-08-29 14:29:33 +08:00
|
|
|
|
private readonly UIHorScrollBarEx HBar = new UIHorScrollBarEx();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
public UIDataGridView()
|
|
|
|
|
{
|
|
|
|
|
BackgroundColor = UIColor.White;
|
|
|
|
|
GridColor = UIColor.Blue;
|
2023-05-25 10:51:41 +08:00
|
|
|
|
base.Font = UIStyles.Font();
|
2020-08-22 22:56:03 +08:00
|
|
|
|
base.DoubleBuffered = true;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-07-15 21:26:07 +08:00
|
|
|
|
VBar.Parent = this;
|
|
|
|
|
VBar.Visible = false;
|
2020-08-29 14:29:33 +08:00
|
|
|
|
HBar.FillColor = VBar.FillColor = UIColor.LightBlue;
|
2020-07-15 21:26:07 +08:00
|
|
|
|
VBar.ForeColor = UIColor.Blue;
|
|
|
|
|
VBar.StyleCustomMode = true;
|
|
|
|
|
VBar.ValueChanged += VBarValueChanged;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
VBar.ShowLeftLine = true;
|
2020-07-18 22:06:58 +08:00
|
|
|
|
|
|
|
|
|
HBar.Parent = this;
|
|
|
|
|
HBar.Visible = false;
|
|
|
|
|
HBar.ForeColor = UIColor.Blue;
|
|
|
|
|
HBar.StyleCustomMode = true;
|
|
|
|
|
HBar.ValueChanged += HBar_ValueChanged;
|
|
|
|
|
|
2020-07-15 21:26:07 +08:00
|
|
|
|
SetBarPosition();
|
2020-07-18 22:06:58 +08:00
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
//支持自定义标题行风格
|
|
|
|
|
EnableHeadersVisualStyles = false;
|
|
|
|
|
|
|
|
|
|
//标题行风格
|
|
|
|
|
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.BackColor = UIColor.Blue;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.ForeColor = UIColor.White;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;
|
2023-05-25 10:51:41 +08:00
|
|
|
|
ColumnHeadersDefaultCellStyle.Font = UIStyles.Font();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2021-04-29 18:12:24 +08:00
|
|
|
|
//行头部颜色
|
|
|
|
|
RowHeadersDefaultCellStyle.BackColor = UIColor.LightBlue;
|
|
|
|
|
RowHeadersDefaultCellStyle.ForeColor = UIFontColor.Primary;
|
|
|
|
|
RowHeadersDefaultCellStyle.SelectionBackColor = UIColor.Blue;
|
|
|
|
|
RowHeadersDefaultCellStyle.SelectionForeColor = Color.White;
|
2023-05-25 10:51:41 +08:00
|
|
|
|
RowHeadersDefaultCellStyle.Font = UIStyles.Font();
|
2022-03-17 22:31:09 +08:00
|
|
|
|
|
2023-05-25 10:51:41 +08:00
|
|
|
|
RowsDefaultCellStyle.Font = UIStyles.Font();
|
|
|
|
|
DefaultCellStyle.Font = UIStyles.Font();
|
2021-04-29 18:12:24 +08:00
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
//标题行行高,与OnColumnAdded事件配合
|
|
|
|
|
ColumnHeadersHeight = 32;
|
|
|
|
|
|
2020-07-15 21:26:07 +08:00
|
|
|
|
//设置奇偶数行颜色
|
|
|
|
|
StripeEvenColor = UIColor.White;
|
|
|
|
|
StripeOddColor = UIColor.LightBlue;
|
|
|
|
|
|
|
|
|
|
VerticalScrollBar.ValueChanged += VerticalScrollBar_ValueChanged;
|
2020-08-29 14:29:33 +08:00
|
|
|
|
HorizontalScrollBar.ValueChanged += HorizontalScrollBar_ValueChanged;
|
2020-09-02 23:46:45 +08:00
|
|
|
|
VerticalScrollBar.VisibleChanged += VerticalScrollBar_VisibleChanged;
|
|
|
|
|
HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 16:44:38 +08:00
|
|
|
|
private int scrollBarWidth = 0;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")]
|
|
|
|
|
public int ScrollBarWidth
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarWidth;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarWidth = value;
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int scrollBarHandleWidth = 6;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(6), Category("SunnyUI"), Description("垂直滚动条滑块宽度,最小为原生滚动条宽度")]
|
|
|
|
|
public int ScrollBarHandleWidth
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarHandleWidth;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarHandleWidth = value;
|
|
|
|
|
if (VBar != null) VBar.FillWidth = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 17:32:50 +08:00
|
|
|
|
private int scrollBarHeight = 0;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")]
|
|
|
|
|
public int ScrollBarHeight
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarHeight;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarHeight = value;
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int scrollBarHandleHeight = 6;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(6), Category("SunnyUI"), Description("垂直滚动条滑块宽度,最小为原生滚动条宽度")]
|
|
|
|
|
public int ScrollBarHandleHeight
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarHandleHeight;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarHandleHeight = value;
|
|
|
|
|
if (HBar != null) HBar.FillHeight = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 23:27:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 禁止控件跟随窗体缩放
|
|
|
|
|
/// </summary>
|
2022-04-07 16:13:17 +08:00
|
|
|
|
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
|
2022-04-11 13:18:21 +08:00
|
|
|
|
public bool ZoomScaleDisabled { get; set; }
|
2022-04-07 16:13:17 +08:00
|
|
|
|
|
2022-06-23 23:27:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 控件缩放前在其容器里的位置
|
|
|
|
|
/// </summary>
|
2023-02-24 17:13:57 +08:00
|
|
|
|
[Browsable(false), DefaultValue(typeof(Rectangle), "0, 0, 0, 0")]
|
2022-04-11 13:18:21 +08:00
|
|
|
|
public Rectangle ZoomScaleRect { get; set; }
|
2022-04-07 16:13:17 +08:00
|
|
|
|
|
2022-06-23 23:27:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置控件缩放比例
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="scale">缩放比例</param>
|
2022-04-11 13:18:21 +08:00
|
|
|
|
public virtual void SetZoomScale(float scale)
|
2022-04-07 16:13:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 16:03:00 +08:00
|
|
|
|
float ColumnHeadersDefaultCellStyleFontSize = -1;
|
|
|
|
|
float RowHeadersDefaultCellStyleFontSize = -1;
|
|
|
|
|
float DefaultCellStyleFontSize = -1;
|
|
|
|
|
float RowsDefaultCellStyleFontSize = -1;
|
2021-10-16 20:39:12 +08:00
|
|
|
|
|
|
|
|
|
public void SetDPIScale()
|
|
|
|
|
{
|
2023-08-29 16:03:00 +08:00
|
|
|
|
if (ColumnHeadersDefaultCellStyle.Font != null)
|
2021-10-16 20:39:12 +08:00
|
|
|
|
{
|
2023-08-29 16:03:00 +08:00
|
|
|
|
if (ColumnHeadersDefaultCellStyleFontSize < 0) ColumnHeadersDefaultCellStyleFontSize = ColumnHeadersDefaultCellStyle.Font.Size;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.Font = ColumnHeadersDefaultCellStyle.Font.DPIScaleFont(ColumnHeadersDefaultCellStyleFontSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RowHeadersDefaultCellStyle.Font != null)
|
|
|
|
|
{
|
|
|
|
|
if (RowHeadersDefaultCellStyleFontSize < 0) RowHeadersDefaultCellStyleFontSize = RowHeadersDefaultCellStyle.Font.Size;
|
|
|
|
|
RowHeadersDefaultCellStyle.Font = RowHeadersDefaultCellStyle.Font.DPIScaleFont(RowHeadersDefaultCellStyleFontSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DefaultCellStyle.Font != null)
|
|
|
|
|
{
|
|
|
|
|
if (DefaultCellStyleFontSize < 0) DefaultCellStyleFontSize = DefaultCellStyle.Font.Size;
|
|
|
|
|
DefaultCellStyle.Font = DefaultCellStyle.Font.DPIScaleFont(DefaultCellStyleFontSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RowsDefaultCellStyle.Font != null)
|
|
|
|
|
{
|
|
|
|
|
if (RowsDefaultCellStyleFontSize < 0) RowsDefaultCellStyleFontSize = RowsDefaultCellStyle.Font.Size;
|
|
|
|
|
RowsDefaultCellStyle.Font = RowsDefaultCellStyle.Font.DPIScaleFont(RowsDefaultCellStyleFontSize);
|
2021-10-16 20:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 23:07:30 +08:00
|
|
|
|
private readonly Dictionary<string, CellStyle> CellStyles = new Dictionary<string, CellStyle>();
|
|
|
|
|
|
|
|
|
|
public class CellStyle
|
|
|
|
|
{
|
|
|
|
|
public int Row { get; set; }
|
|
|
|
|
|
|
|
|
|
public int Col { get; set; }
|
|
|
|
|
|
|
|
|
|
public Color BackColor { get; set; }
|
|
|
|
|
|
|
|
|
|
public Color ForeColor { get; set; }
|
|
|
|
|
|
|
|
|
|
public CellStyle(int row, int col, Color backColor, Color foreColor)
|
|
|
|
|
{
|
|
|
|
|
Row = row;
|
|
|
|
|
Col = col;
|
|
|
|
|
BackColor = backColor;
|
|
|
|
|
ForeColor = foreColor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnCellPainting(e);
|
|
|
|
|
|
|
|
|
|
if (CellStyles.Count > 0 && e.ColumnIndex >= 0 && e.RowIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
object obj = e.Value;
|
|
|
|
|
if (obj == null) return;
|
|
|
|
|
|
|
|
|
|
string key = e.RowIndex + "_" + e.ColumnIndex;
|
|
|
|
|
if (CellStyles.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
e.CellStyle.ForeColor = CellStyles[key].ForeColor;
|
|
|
|
|
e.CellStyle.BackColor = CellStyles[key].BackColor;
|
|
|
|
|
e.CellStyle.SelectionForeColor = CellStyles[key].ForeColor;
|
|
|
|
|
e.CellStyle.SelectionBackColor = CellStyles[key].BackColor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetCellStyle(int row, int col, Color backColor, Color foreColor)
|
|
|
|
|
{
|
|
|
|
|
SetCellStyle(new CellStyle(row, col, backColor, foreColor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetCellStyle(CellStyle style)
|
|
|
|
|
{
|
|
|
|
|
string key = style.Row + "_" + style.Col;
|
|
|
|
|
if (CellStyles.ContainsKey(key))
|
|
|
|
|
CellStyles[key] = style;
|
|
|
|
|
else
|
|
|
|
|
CellStyles.Add(key, style);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearCellStyles()
|
|
|
|
|
{
|
|
|
|
|
CellStyles.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearCellStyle(int row, int col)
|
|
|
|
|
{
|
|
|
|
|
string key = row + "_" + col;
|
|
|
|
|
if (CellStyles.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
CellStyles.Remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 12:54:26 +08:00
|
|
|
|
public void SetRowHeight(int height)
|
2021-04-29 18:12:24 +08:00
|
|
|
|
{
|
2022-06-11 12:54:26 +08:00
|
|
|
|
AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
|
|
|
|
|
RowTemplate.Height = height;
|
2021-04-29 18:12:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 12:54:26 +08:00
|
|
|
|
public void SetColumnHeadersHeight(int height)
|
2021-04-29 18:12:24 +08:00
|
|
|
|
{
|
2022-06-11 12:54:26 +08:00
|
|
|
|
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
|
|
|
|
|
ColumnHeadersHeight = height;
|
2021-04-29 18:12:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 23:46:45 +08:00
|
|
|
|
private void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SetScrollInfo();
|
2020-07-15 21:26:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
2020-08-08 21:55:50 +08:00
|
|
|
|
//自动生成行
|
|
|
|
|
AutoGenerateColumns = false;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
//列占满行
|
|
|
|
|
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
//禁止调整数据行行高
|
2020-07-15 21:26:07 +08:00
|
|
|
|
AllowUserToResizeRows = false;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
//允许调整标题行行宽
|
|
|
|
|
AllowUserToResizeColumns = true;
|
|
|
|
|
|
|
|
|
|
//禁用最后一行空白,自动新增行
|
|
|
|
|
AllowUserToAddRows = false;
|
2020-07-15 21:26:07 +08:00
|
|
|
|
AllowUserToDeleteRows = false;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
//不显示表格线
|
|
|
|
|
CellBorderStyle = DataGridViewCellBorderStyle.None;
|
|
|
|
|
|
|
|
|
|
//禁止行多选
|
2022-04-17 23:34:44 +08:00
|
|
|
|
MultiSelect = false;
|
2020-08-08 21:55:50 +08:00
|
|
|
|
|
|
|
|
|
//不显示数据行标题
|
|
|
|
|
RowHeadersVisible = false;
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
//禁止只读
|
2020-07-15 21:26:07 +08:00
|
|
|
|
//ReadOnly = false;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
//行选
|
|
|
|
|
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
2020-07-15 21:26:07 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2023-02-19 21:06:23 +08:00
|
|
|
|
private bool isLightMode = false;
|
|
|
|
|
public void LightMode()
|
|
|
|
|
{
|
|
|
|
|
AllowUserToAddRows = false;
|
|
|
|
|
AllowUserToDeleteRows = false;
|
|
|
|
|
AllowUserToResizeColumns = false;
|
|
|
|
|
AllowUserToResizeRows = false;
|
|
|
|
|
AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
|
|
|
|
|
BackgroundColor = System.Drawing.Color.White;
|
|
|
|
|
BorderStyle = System.Windows.Forms.BorderStyle.None;
|
|
|
|
|
CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal;
|
|
|
|
|
ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
|
|
|
|
|
StripeOddColor = System.Drawing.Color.White;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.BackColor = Color.White;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.White;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.Black;
|
|
|
|
|
SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
|
|
|
|
|
GridColor = Color.LightGray;
|
|
|
|
|
isLightMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 14:29:33 +08:00
|
|
|
|
private void VerticalScrollBar_ValueChanged(object sender, EventArgs e)
|
2020-07-15 21:26:07 +08:00
|
|
|
|
{
|
2020-08-29 14:29:33 +08:00
|
|
|
|
VBar.Value = FirstDisplayedScrollingRowIndex;
|
2021-04-20 23:30:23 +08:00
|
|
|
|
VerticalScrollBarChanged?.Invoke(this, e);
|
2020-08-29 14:29:33 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-08-29 14:29:33 +08:00
|
|
|
|
private void VBarValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-07-29 10:21:21 +08:00
|
|
|
|
if (RowCount == 0) return;
|
|
|
|
|
int idx = VBar.Value;
|
|
|
|
|
if (idx < 0) idx = 0;
|
|
|
|
|
if (idx >= RowCount) idx = RowCount - 1;
|
2023-07-11 23:25:36 +08:00
|
|
|
|
|
2023-07-12 10:01:33 +08:00
|
|
|
|
int lastFrozen = GetFrozenBottomIndex();
|
|
|
|
|
if (Rows[0].Frozen)
|
|
|
|
|
{
|
|
|
|
|
if (RowCount > lastFrozen + 1)
|
|
|
|
|
{
|
|
|
|
|
lastFrozen += 1;
|
|
|
|
|
FirstDisplayedScrollingRowIndex = Math.Max(idx, lastFrozen);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FirstDisplayedScrollingRowIndex = idx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetFrozenBottomIndex()
|
|
|
|
|
{
|
2023-07-11 23:25:36 +08:00
|
|
|
|
int lastFrozen = 0;
|
|
|
|
|
if (Rows[0].Frozen)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Rows.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Rows[i].Frozen)
|
|
|
|
|
{
|
|
|
|
|
lastFrozen = i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 10:01:33 +08:00
|
|
|
|
return lastFrozen;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 14:29:33 +08:00
|
|
|
|
private void HorizontalScrollBar_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HBar.Value = HorizontalScrollBar.Value;
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2020-08-29 14:29:33 +08:00
|
|
|
|
private void HBar_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HorizontalScrollBar.Value = HBar.Value;
|
|
|
|
|
HorizontalScrollingOffset = HBar.Value;
|
2021-04-20 23:30:23 +08:00
|
|
|
|
HorizontalScrollBarChanged?.Invoke(this, e);
|
2020-08-29 14:29:33 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2021-04-20 23:30:23 +08:00
|
|
|
|
public event EventHandler HorizontalScrollBarChanged;
|
|
|
|
|
|
|
|
|
|
public event EventHandler VerticalScrollBarChanged;
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public void SetScrollInfo()
|
|
|
|
|
{
|
2020-07-18 22:06:58 +08:00
|
|
|
|
if (VBar == null || HBar == null)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 22:56:03 +08:00
|
|
|
|
if (RowCount > DisplayedRowCount(false))
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2020-08-22 22:56:03 +08:00
|
|
|
|
VBar.Maximum = RowCount - DisplayedRowCount(false);
|
2020-07-15 21:26:07 +08:00
|
|
|
|
VBar.Value = FirstDisplayedScrollingRowIndex;
|
2022-07-29 10:21:21 +08:00
|
|
|
|
VBar.Visible = ScrollBars == ScrollBars.Vertical || ScrollBars == ScrollBars.Both;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-15 21:26:07 +08:00
|
|
|
|
VBar.Visible = false;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
2020-07-18 22:06:58 +08:00
|
|
|
|
|
2020-08-29 14:29:33 +08:00
|
|
|
|
if (HorizontalScrollBar.Visible)
|
2020-07-18 22:06:58 +08:00
|
|
|
|
{
|
2020-08-29 14:29:33 +08:00
|
|
|
|
HBar.Maximum = HorizontalScrollBar.Maximum;
|
|
|
|
|
HBar.Value = HorizontalScrollBar.Value;
|
2020-09-29 21:36:45 +08:00
|
|
|
|
HBar.BoundsWidth = HorizontalScrollBar.LargeChange;
|
|
|
|
|
HBar.LargeChange = HorizontalScrollBar.LargeChange;//.Maximum / VisibleColumnCount();
|
2022-07-29 10:21:21 +08:00
|
|
|
|
HBar.Visible = ScrollBars == ScrollBars.Horizontal || ScrollBars == ScrollBars.Both;
|
2020-07-18 22:06:58 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HBar.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetBarPosition();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 15:04:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 重载绘图
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">绘图参数</param>
|
2020-08-29 14:29:33 +08:00
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnPaint(e);
|
|
|
|
|
|
2022-06-11 12:54:26 +08:00
|
|
|
|
if (BorderStyle == BorderStyle.FixedSingle)
|
2020-08-29 14:29:33 +08:00
|
|
|
|
{
|
|
|
|
|
Color color = RectColor;
|
|
|
|
|
color = Enabled ? color : UIDisableColor.Fill;
|
|
|
|
|
e.Graphics.DrawRectangle(color, new Rectangle(0, 0, Width - 1, Height - 1));
|
|
|
|
|
}
|
2023-02-19 21:06:23 +08:00
|
|
|
|
|
|
|
|
|
if (isLightMode)
|
|
|
|
|
{
|
|
|
|
|
e.Graphics.DrawLine(GridColor, 0, ColumnHeadersHeight - 1, Width, ColumnHeadersHeight - 1);
|
|
|
|
|
}
|
2020-08-29 14:29:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
protected override void OnMouseWheel(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnMouseWheel(e);
|
2021-03-25 14:49:21 +08:00
|
|
|
|
|
|
|
|
|
if (VBar.Visible && ScrollMode == UIDataGridViewScrollMode.Page)
|
|
|
|
|
{
|
|
|
|
|
if (e.Delta > 10)
|
|
|
|
|
{
|
|
|
|
|
var lineCount = Rows.GetLastRow(DataGridViewElementStates.Displayed) - FirstDisplayedScrollingRowIndex;
|
|
|
|
|
VBar.SetValue(VBar.Value - lineCount + 3);
|
|
|
|
|
}
|
|
|
|
|
else if (e.Delta < -10)
|
|
|
|
|
{
|
|
|
|
|
var lineCount = FirstDisplayedScrollingRowIndex - Rows.GetLastRow(DataGridViewElementStates.Displayed);
|
|
|
|
|
VBar.SetValue(VBar.Value - lineCount - 3);
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 14:49:21 +08:00
|
|
|
|
[Description("垂直滚动条滚动方式"), Category("SunnyUI")]
|
|
|
|
|
[DefaultValue(UIDataGridViewScrollMode.Normal)]
|
|
|
|
|
public UIDataGridViewScrollMode ScrollMode { get; set; } = UIDataGridViewScrollMode.Normal;
|
|
|
|
|
|
|
|
|
|
public enum UIDataGridViewScrollMode
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 正常
|
|
|
|
|
/// </summary>
|
|
|
|
|
Normal,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 翻页
|
|
|
|
|
/// </summary>
|
|
|
|
|
Page
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 23:25:19 +08:00
|
|
|
|
protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnRowsAdded(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnRowsRemoved(DataGridViewRowsRemovedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnRowsRemoved(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 17:06:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 重载控件尺寸变更
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">参数</param>
|
2020-05-11 21:11:29 +08:00
|
|
|
|
protected override void OnSizeChanged(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnSizeChanged(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
SetBarPosition();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 21:01:53 +08:00
|
|
|
|
protected override void OnColumnStateChanged(DataGridViewColumnStateChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnColumnStateChanged(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnColumnRemoved(DataGridViewColumnEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnColumnRemoved(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
private void SetBarPosition()
|
|
|
|
|
{
|
2021-11-03 22:41:11 +08:00
|
|
|
|
if (VBar == null || HBar == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 16:44:38 +08:00
|
|
|
|
int barWidth = Math.Max(ScrollBarInfo.VerticalScrollBarWidth(), ScrollBarWidth);
|
2023-06-28 17:32:50 +08:00
|
|
|
|
int barHeight = Math.Max(ScrollBarInfo.HorizontalScrollBarHeight(), ScrollBarHeight);
|
2022-10-14 16:44:38 +08:00
|
|
|
|
|
2022-06-11 12:54:26 +08:00
|
|
|
|
if (BorderStyle == BorderStyle.FixedSingle)
|
2020-08-22 22:56:03 +08:00
|
|
|
|
{
|
2022-10-14 16:44:38 +08:00
|
|
|
|
VBar.Left = Width - barWidth - 2;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
VBar.Top = 1;
|
2022-10-14 16:44:38 +08:00
|
|
|
|
VBar.Width = barWidth + 1;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
VBar.Height = Height - 2;
|
|
|
|
|
VBar.BringToFront();
|
|
|
|
|
|
2022-07-11 23:22:32 +08:00
|
|
|
|
HBar.Left = 1;
|
2023-06-28 17:32:50 +08:00
|
|
|
|
HBar.Height = barHeight + 1;
|
2022-07-11 23:22:32 +08:00
|
|
|
|
HBar.Width = Width - (VBar.Visible ? VBar.Width : 0) - 2;
|
|
|
|
|
HBar.Top = Height - HBar.Height - 1;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
HBar.BringToFront();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-14 16:44:38 +08:00
|
|
|
|
VBar.Left = Width - barWidth - 1;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
VBar.Top = 0;
|
2022-10-14 16:44:38 +08:00
|
|
|
|
VBar.Width = barWidth + 1;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
VBar.Height = Height;
|
|
|
|
|
VBar.BringToFront();
|
|
|
|
|
|
|
|
|
|
HBar.Left = 0;
|
2023-06-28 17:32:50 +08:00
|
|
|
|
HBar.Height = barHeight + 1;
|
2020-08-22 22:56:03 +08:00
|
|
|
|
HBar.Width = Width - (VBar.Visible ? VBar.Width : 0);
|
|
|
|
|
HBar.Top = Height - HBar.Height;
|
|
|
|
|
HBar.BringToFront();
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnColumnAdded(e);
|
|
|
|
|
|
|
|
|
|
//设置可调整标题行行高
|
|
|
|
|
if (ColumnHeadersHeightSizeMode == DataGridViewColumnHeadersHeightSizeMode.AutoSize)
|
|
|
|
|
{
|
|
|
|
|
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
|
|
|
|
|
}
|
2020-10-19 21:01:53 +08:00
|
|
|
|
|
|
|
|
|
SetScrollInfo();
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UIStyle _style = UIStyle.Blue;
|
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 主题样式
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public UIStyle Style
|
|
|
|
|
{
|
|
|
|
|
get => _style;
|
|
|
|
|
set => SetStyle(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(typeof(Color), "White")]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("偶数行显示颜色"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public Color StripeEvenColor
|
|
|
|
|
{
|
|
|
|
|
get => RowsDefaultCellStyle.BackColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
RowsDefaultCellStyle.BackColor = value;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 17:16:40 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "243, 249, 255")]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("奇数行显示颜色"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public Color StripeOddColor
|
|
|
|
|
{
|
|
|
|
|
get => AlternatingRowsDefaultCellStyle.BackColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
AlternatingRowsDefaultCellStyle.BackColor = value;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 14:08:09 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置主题样式
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="style">主题样式</param>
|
|
|
|
|
private void SetStyle(UIStyle style)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2022-03-20 17:16:40 +08:00
|
|
|
|
if (!style.IsCustom())
|
|
|
|
|
{
|
|
|
|
|
SetStyleColor(style.Colors());
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 14:08:09 +08:00
|
|
|
|
_style = style == UIStyle.Inherited ? UIStyle.Inherited : UIStyle.Custom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetInheritedStyle(UIStyle style)
|
|
|
|
|
{
|
|
|
|
|
SetStyle(style);
|
|
|
|
|
_style = UIStyle.Inherited;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetStyleColor(UIBaseStyle uiColor)
|
|
|
|
|
{
|
2022-03-21 23:25:57 +08:00
|
|
|
|
BackgroundColor = uiColor.PlainColor;
|
|
|
|
|
|
2021-01-22 11:05:37 +08:00
|
|
|
|
//列头部颜色
|
2022-03-20 17:16:40 +08:00
|
|
|
|
ColumnHeadersDefaultCellStyle.BackColor = uiColor.GridTitleColor;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.ForeColor = uiColor.GridTitleForeColor;
|
|
|
|
|
ColumnHeadersDefaultCellStyle.SelectionBackColor = uiColor.GridTitleColor;
|
2021-01-22 11:05:37 +08:00
|
|
|
|
|
|
|
|
|
//行头部颜色
|
|
|
|
|
RowHeadersDefaultCellStyle.BackColor = uiColor.PlainColor;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
RowHeadersDefaultCellStyle.ForeColor = uiColor.GridForeColor;
|
2021-04-29 18:12:24 +08:00
|
|
|
|
RowHeadersDefaultCellStyle.SelectionBackColor = uiColor.RectColor;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
RowHeadersDefaultCellStyle.SelectionForeColor = uiColor.GridForeColor;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2022-03-16 15:10:14 +08:00
|
|
|
|
//数据单元格选中颜色
|
2020-05-11 21:11:29 +08:00
|
|
|
|
DefaultCellStyle.SelectionBackColor = uiColor.GridSelectedColor;
|
|
|
|
|
DefaultCellStyle.SelectionForeColor = uiColor.GridSelectedForeColor;
|
2022-03-16 15:10:14 +08:00
|
|
|
|
DefaultCellStyle.BackColor = uiColor.GridStripeEvenColor;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
DefaultCellStyle.ForeColor = uiColor.GridForeColor;
|
2022-03-16 15:10:14 +08:00
|
|
|
|
|
|
|
|
|
//数据行选中颜色
|
|
|
|
|
RowsDefaultCellStyle.SelectionBackColor = uiColor.GridSelectedColor;
|
|
|
|
|
RowsDefaultCellStyle.SelectionForeColor = uiColor.GridSelectedForeColor;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
RowsDefaultCellStyle.ForeColor = uiColor.GridForeColor;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2022-03-20 17:16:40 +08:00
|
|
|
|
GridColor = uiColor.GridLineColor;
|
|
|
|
|
RectColor = uiColor.RectColor;
|
2022-03-21 23:25:57 +08:00
|
|
|
|
RowsDefaultCellStyle.BackColor = uiColor.GridStripeEvenColor;
|
|
|
|
|
AlternatingRowsDefaultCellStyle.BackColor = uiColor.GridStripeOddColor;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
StripeEvenColor = uiColor.GridStripeEvenColor;
|
|
|
|
|
StripeOddColor = uiColor.GridStripeOddColor;
|
|
|
|
|
|
2022-04-16 21:19:31 +08:00
|
|
|
|
if (HBar != null)
|
|
|
|
|
{
|
2022-07-11 23:22:32 +08:00
|
|
|
|
HBar.ForeColor = uiColor.GridBarForeColor;
|
2022-04-16 21:19:31 +08:00
|
|
|
|
HBar.HoverColor = uiColor.ButtonFillHoverColor;
|
|
|
|
|
HBar.PressColor = uiColor.ButtonFillPressColor;
|
|
|
|
|
HBar.FillColor = uiColor.GridBarFillColor;
|
2022-07-11 23:22:32 +08:00
|
|
|
|
//HBar.RectColor = uiColor.RectColor;
|
|
|
|
|
scrollBarColor = uiColor.GridBarForeColor;
|
2022-04-16 21:19:31 +08:00
|
|
|
|
scrollBarBackColor = uiColor.GridBarFillColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (VBar != null)
|
|
|
|
|
{
|
2022-07-11 23:22:32 +08:00
|
|
|
|
VBar.ForeColor = uiColor.GridBarForeColor;
|
2022-04-16 21:19:31 +08:00
|
|
|
|
VBar.HoverColor = uiColor.ButtonFillHoverColor;
|
|
|
|
|
VBar.PressColor = uiColor.ButtonFillPressColor;
|
|
|
|
|
VBar.FillColor = uiColor.GridBarFillColor;
|
2022-07-11 23:22:32 +08:00
|
|
|
|
scrollBarRectColor = VBar.RectColor = uiColor.RectColor;
|
|
|
|
|
scrollBarColor = uiColor.GridBarForeColor;
|
2022-04-16 21:19:31 +08:00
|
|
|
|
scrollBarBackColor = uiColor.GridBarFillColor;
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自定义主题风格
|
|
|
|
|
/// </summary>
|
2023-11-06 14:08:09 +08:00
|
|
|
|
[DefaultValue(false), Browsable(false)]
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[Description("获取或设置可以自定义主题风格"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public bool StyleCustomMode { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Version => UIGlobal.Version;
|
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tag字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DefaultValue(null)]
|
|
|
|
|
[Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public string TagString { get; set; }
|
|
|
|
|
|
2022-06-11 12:54:26 +08:00
|
|
|
|
protected override void OnCellBorderStyleChanged(EventArgs e)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2022-06-11 12:54:26 +08:00
|
|
|
|
base.OnCellBorderStyleChanged(e);
|
|
|
|
|
VBar.ShowLeftLine = CellBorderStyle == DataGridViewCellBorderStyle.Single;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Color _rectColor = UIColor.Blue;
|
|
|
|
|
|
2020-08-08 21:55:50 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "80, 160, 255")]
|
|
|
|
|
[Description("边框颜色"), Category("SunnyUI")]
|
2020-05-11 21:11:29 +08:00
|
|
|
|
public Color RectColor
|
|
|
|
|
{
|
|
|
|
|
get => _rectColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_rectColor != value)
|
|
|
|
|
{
|
|
|
|
|
_rectColor = value;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
|
public int SelectedIndex
|
|
|
|
|
{
|
2022-01-21 09:52:05 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
2022-02-27 22:04:59 +08:00
|
|
|
|
return CurrentRow != null ? CurrentRow.Index : -1;
|
2022-01-21 09:52:05 +08:00
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-03-01 23:04:17 +08:00
|
|
|
|
//BindingContext[DataSource].Position = value;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
if (Rows.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value >= 0 && value < Rows.Count)
|
|
|
|
|
{
|
2022-01-21 09:52:05 +08:00
|
|
|
|
foreach (DataGridViewRow row in SelectedRows)
|
|
|
|
|
{
|
|
|
|
|
row.Selected = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
Rows[value].Selected = true;
|
2023-07-12 10:01:33 +08:00
|
|
|
|
|
|
|
|
|
int lastFrozen = GetFrozenBottomIndex();
|
|
|
|
|
if (Rows[0].Frozen)
|
|
|
|
|
{
|
|
|
|
|
if (RowCount > lastFrozen + 1)
|
|
|
|
|
{
|
|
|
|
|
lastFrozen += 1;
|
|
|
|
|
FirstDisplayedScrollingRowIndex = Math.Max(value, lastFrozen);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FirstDisplayedScrollingRowIndex = value;
|
|
|
|
|
}
|
2022-01-21 09:52:05 +08:00
|
|
|
|
|
|
|
|
|
if (selectedIndex >= 0 && selectedIndex <= Rows.Count)
|
|
|
|
|
jumpIndex = selectedIndex;
|
|
|
|
|
|
|
|
|
|
selectedIndex = value;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
SelectIndexChange?.Invoke(this, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 09:52:05 +08:00
|
|
|
|
private int jumpIndex = -1;
|
|
|
|
|
|
2020-05-11 21:11:29 +08:00
|
|
|
|
protected override void OnDataSourceChanged(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnDataSourceChanged(e);
|
|
|
|
|
SetScrollInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate void OnSelectIndexChange(object sender, int index);
|
|
|
|
|
|
|
|
|
|
public event OnSelectIndexChange SelectIndexChange;
|
|
|
|
|
|
2022-01-21 09:52:05 +08:00
|
|
|
|
protected override void OnRowEnter(DataGridViewCellEventArgs e)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
2022-01-21 09:52:05 +08:00
|
|
|
|
base.OnRowEnter(e);
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
2022-01-21 09:52:05 +08:00
|
|
|
|
if (e.RowIndex == jumpIndex)
|
|
|
|
|
{
|
|
|
|
|
jumpIndex = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectedIndex != e.RowIndex)
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
|
|
|
|
selectedIndex = e.RowIndex;
|
|
|
|
|
SelectIndexChange?.Invoke(this, e.RowIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 09:52:05 +08:00
|
|
|
|
private int selectedIndex = -1;
|
2020-05-11 21:11:29 +08:00
|
|
|
|
|
|
|
|
|
public DataGridViewColumn AddColumn(string columnName, string dataPropertyName, int fillWeight = 100, DataGridViewContentAlignment alignment = DataGridViewContentAlignment.MiddleCenter, bool readOnly = true)
|
|
|
|
|
{
|
|
|
|
|
DataGridViewColumn column = new DataGridViewTextBoxColumn();
|
|
|
|
|
column.HeaderText = columnName;
|
|
|
|
|
column.DataPropertyName = dataPropertyName;
|
|
|
|
|
column.Name = columnName;
|
|
|
|
|
column.ReadOnly = readOnly;
|
|
|
|
|
column.FillWeight = fillWeight;
|
|
|
|
|
column.SortMode = DataGridViewColumnSortMode.NotSortable;
|
|
|
|
|
column.DefaultCellStyle.Alignment = alignment;
|
|
|
|
|
Columns.Add(column);
|
|
|
|
|
return column;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DataGridViewColumn AddCheckBoxColumn(string columnName, string dataPropertyName, int fillWeight = 100, bool readOnly = true)
|
|
|
|
|
{
|
|
|
|
|
DataGridViewColumn column = new DataGridViewCheckBoxColumn();
|
|
|
|
|
column.HeaderText = columnName;
|
|
|
|
|
column.DataPropertyName = dataPropertyName;
|
|
|
|
|
column.Name = columnName;
|
|
|
|
|
column.ReadOnly = readOnly;
|
|
|
|
|
column.FillWeight = fillWeight;
|
|
|
|
|
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
|
|
|
|
Columns.Add(column);
|
|
|
|
|
return column;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:01:40 +08:00
|
|
|
|
public DataGridViewColumn AddButtonColumn(string columnName, string dataPropertyName, int fillWeight = 100, bool readOnly = true)
|
|
|
|
|
{
|
|
|
|
|
DataGridViewColumn column = new DataGridViewButtonColumn();
|
|
|
|
|
column.HeaderText = columnName;
|
|
|
|
|
column.DataPropertyName = dataPropertyName;
|
|
|
|
|
column.Name = columnName;
|
|
|
|
|
column.ReadOnly = readOnly;
|
|
|
|
|
column.FillWeight = fillWeight;
|
|
|
|
|
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
|
|
|
|
Columns.Add(column);
|
|
|
|
|
return column;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 22:12:52 +08:00
|
|
|
|
public virtual void ClearRows()
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
|
|
|
|
if (DataSource != null)
|
|
|
|
|
{
|
|
|
|
|
DataSource = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rows.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 22:12:52 +08:00
|
|
|
|
public virtual void ClearColumns()
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
|
|
|
|
Columns.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 22:12:52 +08:00
|
|
|
|
public virtual void ClearAll()
|
2020-05-11 21:11:29 +08:00
|
|
|
|
{
|
|
|
|
|
ClearRows();
|
|
|
|
|
ClearColumns();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 11:19:55 +08:00
|
|
|
|
public int AddRow(params object[] values)
|
|
|
|
|
{
|
|
|
|
|
return Rows.Add(values);
|
|
|
|
|
}
|
2021-04-01 21:56:09 +08:00
|
|
|
|
|
|
|
|
|
protected override bool ProcessDialogKey(Keys keyData)
|
|
|
|
|
{
|
|
|
|
|
if (EnterAsTab)
|
|
|
|
|
{
|
|
|
|
|
Keys key = (keyData & Keys.KeyCode);
|
|
|
|
|
if (key == Keys.Enter)
|
|
|
|
|
{
|
|
|
|
|
//交由自定义控件处理
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.ProcessDialogKey(keyData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (EnterAsTab)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.Enter)
|
|
|
|
|
{
|
|
|
|
|
return this.ProcessTabKey(e.KeyData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.ProcessDataGridViewKey(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
[Description("编辑输入时,用Enter键代替Tab键跳到下一个单元格"), Category("SunnyUI")]
|
|
|
|
|
public bool EnterAsTab { get; set; }
|
2022-04-16 21:19:31 +08:00
|
|
|
|
|
|
|
|
|
private Color scrollBarColor = Color.FromArgb(80, 160, 255);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 填充颜色,当值为背景色或透明色或空值则不填充
|
|
|
|
|
/// </summary>
|
2022-05-26 11:03:06 +08:00
|
|
|
|
[Description("滚动条填充颜色"), Category("SunnyUI")]
|
2022-04-16 21:19:31 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "80, 160, 255")]
|
|
|
|
|
public Color ScrollBarColor
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarColor = value;
|
|
|
|
|
HBar.HoverColor = HBar.PressColor = HBar.ForeColor = value;
|
|
|
|
|
VBar.HoverColor = VBar.PressColor = VBar.ForeColor = value;
|
|
|
|
|
_style = UIStyle.Custom;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 23:22:32 +08:00
|
|
|
|
private Color scrollBarRectColor = Color.FromArgb(80, 160, 255);
|
|
|
|
|
|
|
|
|
|
public Color ScrollBarRectColor
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarRectColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarRectColor = value;
|
|
|
|
|
VBar.RectColor = value;
|
|
|
|
|
_style = UIStyle.Custom;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 21:19:31 +08:00
|
|
|
|
private Color scrollBarBackColor = Color.FromArgb(243, 249, 255);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 填充颜色,当值为背景色或透明色或空值则不填充
|
|
|
|
|
/// </summary>
|
2022-05-26 11:03:06 +08:00
|
|
|
|
[Description("滚动条背景颜色"), Category("SunnyUI")]
|
2022-04-16 21:19:31 +08:00
|
|
|
|
[DefaultValue(typeof(Color), "243, 249, 255")]
|
|
|
|
|
public Color ScrollBarBackColor
|
|
|
|
|
{
|
|
|
|
|
get => scrollBarBackColor;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
scrollBarBackColor = value;
|
|
|
|
|
HBar.FillColor = value;
|
|
|
|
|
VBar.FillColor = value;
|
|
|
|
|
_style = UIStyle.Custom;
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class UIDataGridViewHelper
|
|
|
|
|
{
|
|
|
|
|
public static DataGridViewColumn SetFixedMode(this DataGridViewColumn column, int width)
|
|
|
|
|
{
|
|
|
|
|
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
|
|
|
|
|
column.Width = width;
|
|
|
|
|
return column;
|
|
|
|
|
}
|
2020-05-27 17:26:21 +08:00
|
|
|
|
|
|
|
|
|
public static DataGridViewColumn SetSortMode(this DataGridViewColumn column, DataGridViewColumnSortMode sortMode = DataGridViewColumnSortMode.Automatic)
|
|
|
|
|
{
|
|
|
|
|
column.SortMode = sortMode;
|
|
|
|
|
return column;
|
|
|
|
|
}
|
2022-03-28 15:39:08 +08:00
|
|
|
|
|
|
|
|
|
public static bool IsDBNull(this DataGridViewCell cell)
|
|
|
|
|
{
|
|
|
|
|
return cell.Value is DBNull;
|
|
|
|
|
}
|
2020-05-11 21:11:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|