From 51c6da4833f2fb3e1615475e680c7a77e67f413e Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 27 Aug 2024 14:14:36 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIDataGridView:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=B1=9E=E6=80=A7AutoScrollToBottom=EF=BC=8C=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=B6=E6=98=AF=E5=90=A6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E5=88=B0=E6=9C=80=E5=90=8E=E4=B8=80=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UIDataGridView.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/SunnyUI/Controls/UIDataGridView.cs b/SunnyUI/Controls/UIDataGridView.cs index 7c21fddd..6b788ced 100644 --- a/SunnyUI/Controls/UIDataGridView.cs +++ b/SunnyUI/Controls/UIDataGridView.cs @@ -48,6 +48,7 @@ * 2023-07-12: V3.4.0 修复了有冻结行时垂直滚动条点击时出错的问题 * 2023-11-05: V3.5.2 重构主题 * 2024-06-19: V3.6.7 增加AddDateTimeColumn,解决默认时间列不显示秒数的问题 + * 2024-08-27: V3.7.0 增加属性AutoScrollToBottom,数据更新时是否自动滚动到最后一行 ******************************************************************************/ using System; @@ -1041,6 +1042,26 @@ namespace Sunny.UI } } + + [DefaultValue(false), Category("SunnyUI"), Description("指示当有新数据添加到DataGridView时,是否自动滚动到最后一行")] + public bool AutoScrollToBottom { get; set; } + + protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e) + { + base.OnDataBindingComplete(e); + //是否自动滚动到最后一行 + if (AutoScrollToBottom && this.RowCount > 0) + { + //不选中单元格或行 + this.CurrentCell = null; + //选中最后一行 + this.Rows[this.RowCount - 1].Selected = true; + //滚动到最后一行 + this.FirstDisplayedScrollingRowIndex = this.RowCount - 1; + //如果需要滚动到底部(右侧),使用下面的代码 + //this.FirstDisplayedCell = this.Rows[this.RowCount - 1].Cells[this.Columns.Count - 1]; + } + } } public static class UIDataGridViewHelper