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

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

问题描述

我有一个DataGridView具有用户可定义数量的列数(从6-60)。在较高端,网格中的数据量超过可以一次显示在屏幕上的数据。我有一个图形与数据。我想保持两者同步,使图上的特定时间T与网格中​​的同一时间垂直排列。



为了做到这一点,我想使DGV足够宽,以避免水平滚动条,图形同样宽,然后卸载滚动到集装箱控制。但是,我找不到一种直接获取宽度的方法来设置DGV,以便从中删除滚动条。

解决方案

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



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

  ///< summary> 
///在控件的垂直滚动条显示之前,返回DataGridView的最小宽度(以像素为单位)。
///< / summary>
private int GetDgvMinWidth(DataGridView dgv){
//为BorderStyles除了None之外的边框添加两个像素。
var controlBorderWidth =(dgv.BorderStyle == BorderStyle.None)? 0:2;

//返回所有列的宽度加上行标题,并为DGV的BorderStyle进行调整。
return dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+ dgv.RowHeadersWidth + controlBorderWidth;
}


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.

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.

解决方案

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.

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天全站免登陆