【发布时间】:2017-05-02 07:04:01
【问题描述】:
我在Panel 中有一个DataGridView,所以我可以平滑滚动。
DataGridView 将其高度设置为行的总高度。
我在 DGV 上有一个 CellContentClick 事件,该事件仅在 e.ColumnIndex == 0 时做出反应。
目前我正在加载大约 1700 行。在大约 32569 像素高度的第 1489 行之后,这是我能够点击事件触发的最后一行。
[编辑] 2017 年 5 月 2 日 9:59PM AU
添加代码以显示正在发生的事情。在表单中,我添加了一个自定义面板(下面的代码),AutoScroll 设置为 true,然后放置一个 DataGridView,滚动条设置为 none。
面板类
public class CustomPanel : System.Windows.Forms.Panel
{
protected override System.Drawing.Point ScrollToControl(System.Windows.Forms.Control activeControl)
{
// Returning the current location prevents the panel from scrolling to the active control when the panel loses and regains focus
return this.DisplayRectangle.Location;
}
}
数据类
public class dgvRecord
{
public double ID { get; set; }
public string TYPE { get; set; }
public string NAME { get; set; }
public DateTime DATE { get; set; }
public dgvRecord() { }
}
表单类
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dgvInPanel
{
public partial class Form1 : Form
{
List<dgvRecord> recList = new List<dgvRecord>();
int normalRowHeight = 0;
public Form1()
{
InitializeComponent();
for (int rCount = 0; rCount < 1800; rCount++)
{
dgvRecord rec = new dgvRecord() { ID = 20170501000000, TYPE = "Asset", NAME = "Joe Bloggs", DATE = new DateTime(2017, 05, 02, 10, 30, 15) };
recList.Add(rec);
}
}
private void Form1_Shown(object sender, EventArgs e)
{
DataGridViewCellStyle imageCellStyle = new DataGridViewCellStyle();
imageCellStyle.SelectionBackColor = dgv.DefaultCellStyle.BackColor;
imageCellStyle.SelectionForeColor = dgv.DefaultCellStyle.ForeColor;
imageCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
DataGridViewCellStyle otherCellStyle = new DataGridViewCellStyle();
otherCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
dgv.AllowUserToResizeRows = false;
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.ReadOnly = true;
DataGridViewCell dgvc_ID = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn dgvTbxCol_ID = new DataGridViewTextBoxColumn()
{
CellTemplate = dgvc_ID,
Name = "ID",
HeaderText = "ID",
DataPropertyName = "ID",
DefaultCellStyle = otherCellStyle,
};
dgv.Columns.Add(dgvTbxCol_ID);
DataGridViewCell dgvc_TYPE = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn dgvTbxCol_TYPE = new DataGridViewTextBoxColumn()
{
CellTemplate = dgvc_TYPE,
Name = "TYPE",
HeaderText = "Type",
DataPropertyName = "TYPE",
AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,
DefaultCellStyle = otherCellStyle,
};
dgv.Columns.Add(dgvTbxCol_TYPE);
DataGridViewCell dgvc_R_NAME = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn dgvTbxCol_R_NAME = new DataGridViewTextBoxColumn()
{
CellTemplate = dgvc_R_NAME,
Name = "R_NAME",
HeaderText = "Name",
MinimumWidth = 110,
DataPropertyName = "NAME",
DefaultCellStyle = otherCellStyle,
};
dgv.Columns.Add(dgvTbxCol_R_NAME);
DataGridViewCell dgvc_DATE = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn dgvTbxCol_DATE = new DataGridViewTextBoxColumn()
{
CellTemplate = dgvc_DATE,
Name = "DATE",
HeaderText = "Date",
MinimumWidth = 135,
DataPropertyName = "DATE",
DefaultCellStyle = otherCellStyle,
};
dgv.Columns.Add(dgvTbxCol_DATE);
dgv.DataSource = recList;
int dgvWidth = 0;
int dgvHeight = 0;
foreach (DataGridViewRow row in dgv.Rows)
{
if (dgvHeight == 0)
{
normalRowHeight = row.Height;
}
dgvHeight += row.Height;
}
dgvWidth = pnlCust.Width - 17;
dgv.Size = new Size(dgvWidth, dgvHeight);
dgv.DataSource = recList;
}
}
}
如果你运行这个,在顶部的某个地方点击一个单元格来选择内容,那应该没问题。
如果你走到底部,尝试做同样的事情我发现它不允许选择,我发现在第 1489 行附近的某个地方它停止工作。 这可能是什么原因造成的?
[EDIT2] 2017 年 5 月 3 日上午 8:00 澳大利亚
我将 DGV 放在面板中的原因是为了获得平滑滚动。如果我可以在 DGV 上平滑滚动,我会使用它。
【问题讨论】:
-
不是所有的理论,一些代码肯定会有所帮助
-
DataGridViews 高度 = 32569? -
这个限制远远超过了你的用户仍然愿意与你交谈的程度。高度限制是 Windows 中的硬限制,鼠标通知消息将 X 和 Y 鼠标坐标编码为 16 位值。 DGV 可以自己显示滚动条,所以这不是必需的。
-
谢谢韩,我明白了,但是它将用作数据审计工具的情况下,大网格就可以了。就仅使用 dgv 本身而言,我尝试过,但我将有一些行的高度可能高达几个正常行,这意味着当向下滚动时,当该行离开可视区域时它会跳转。该面板允许平滑滚动。我还没有遇到过平滑滚动的 dgv 或者是否有可能。
-
在 DataGridView 中使用虚拟模式。
标签: c# winforms datagridview