以编程方式调整 DataGridView 的大小以删除滚动条 [英] Programmatically resize DataGridView to remove scroll bars

查看:40
本文介绍了以编程方式调整 DataGridView 的大小以删除滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView,其中包含用户可定义的数值数据列数(从 ~6-60 之间的任意位置).在较高端,网格中的数据量超过了屏幕上可以同时显示的数据量.我有一个与数据相符的图表.我想让两者保持同步,以便图表上的特定时间 T 与网格中的同一时间垂直对齐.

I have a DataGridView with a user definable number of columns (anywhere from ~6-60) of numerical data. At the higher end that amount of data in the grid exceeds that which can be displayed on screen at once. I have a graph that goes with the data. I would like to keep the two in sync, so that a specific time T on the graph is in line vertically with the same time in the grid.

为了做到这一点,我想让 DGV 刚好足够宽以避免出现水平滚动条,让图形同样宽,然后将滚动卸载到容器控件上.但是,我找不到直接获得宽度的方法,我需要将 DGV 设置为以从中删除滚动条.

To do this I'd like to make the DGV just wide enough to avoid a horizontal scroll bar, have the graph be equally wide, and then offload the scrolling onto a container control. However, I can't find a way to directly get the width I'd need to set the DGV to in order to remove the scroll bar from it.

推荐答案

要防止 DataGridView 显示其水平滚动条,您需要确保 DGV 的宽度不小于其列的宽度加上行标题的宽度.当控件的 BorderStyle 属性不是 None 时,您还需要调整添加到控件宽度(和高度)的两个像素.

To prevent a DataGridView from displaying its horizontal scrollbar you'll need to make sure your DGV's width is not less than the width of its columns plus the row header's width. You'll also need to adjust for the two pixels that are added to the control's width (and height) when its BorderStyle property is not None.

这是一个返回给定 DataGridView 最小值的方法:

Here's a method which will return this minimum value for a given DataGridView:

/// <summary>
/// Return the minimum width in pixels a DataGridView can be before the control's vertical scrollbar would be displayed.
/// </summary>
private int GetDgvMinWidth(DataGridView dgv) {
    // Add two pixels for the border for BorderStyles other than None.
    var controlBorderWidth = (dgv.BorderStyle == BorderStyle.None) ? 0 : 2;

    // Return the width of all columns plus the row header, and adjusted for the DGV's BorderStyle.
    return dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + dgv.RowHeadersWidth + controlBorderWidth;
}

这篇关于以编程方式调整 DataGridView 的大小以删除滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆