Win Forms DataGridView 水平滚动条

发布于 10-21 09:26 字数 189 浏览 8 评论 0原文

是否可以在 Windows Forms 2.0 中永久显示 DataGridView 的水平滚动条。就像我们可以在面板的水平滚动条中所做的那样。

目前,只有当列的宽度总和大于 DataGridView 宽度时,Horizo​​ntal ScrollBar 才可见。但我希望这个滚动条始终可见。

谢谢

Is it possible to show permanently the horizontal scroll bar of a DataGridView in Windows Forms 2.0. Like we can do in the horizontal scroll bar of a Panel.

Currently, the Horizontal ScrollBar is visible only when the total sum of widths of the columns is bigger than the DataGridView width. But I want this scroll bar to be always visible.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子2024-10-28 09:26:24

正如评论中提到的,DataGridView 控制其滚动条,并且如果不需要查看它们,则始终希望隐藏它们,例如,所有单元格都适合网格的可见区域。

但是,有一种方法可以强制 DataGridView 使用反射显示其滚动条,尽管这是一种 hack,我不建议这样做。下面是一个例子:

public Form1()
{
    InitializeComponent();

    // assuming dataGridView1 is a DataGridView control placed on the Form1 form
    PropertyInfo property = dataGridView1.GetType().GetProperty(
        "HorizontalScrollBar", BindingFlags.NonPublic | BindingFlags.Instance);
    if (property != null)
    {
        ScrollBar scrollbar = (ScrollBar)property.GetValue(dataGridView1, null);
        scrollbar.Visible = true;
        scrollbar.VisibleChanged += new EventHandler(ScrollBar_VisibleChanged);
    }
}

void ScrollBar_VisibleChanged(object sender, EventArgs e)
{
    FieldInfo field = dataGridView1.GetType().GetField(
        "layout", BindingFlags.NonPublic | BindingFlags.Instance);
    if (field != null)
    {
        object layoutData = field.GetValue(dataGridView1);
        FieldInfo insideField = layoutData.GetType().GetField(
            "Inside", BindingFlags.Public | BindingFlags.Instance);
        Rectangle rect = (Rectangle)insideField.GetValue(layoutData); 

        ScrollBar scrollBar = (ScrollBar)sender;
        scrollBar.Visible = true;
        scrollBar.SetBounds(
            rect.Left, rect.Height - scrollBar.Height + 1,
            rect.Width, scrollBar.Height);
    }
}

希望这有帮助,问候

As it was mentioned in comments DataGridView controls its scrollbars and always wants to hide them if there is no need in viewing them, e.g. all cells fit into grid's visible area.

However, there is a way to force DataGridView to show its scroll bars using reflection, though it's a hack and I wouldn't recommend doing this. Below is an example:

public Form1()
{
    InitializeComponent();

    // assuming dataGridView1 is a DataGridView control placed on the Form1 form
    PropertyInfo property = dataGridView1.GetType().GetProperty(
        "HorizontalScrollBar", BindingFlags.NonPublic | BindingFlags.Instance);
    if (property != null)
    {
        ScrollBar scrollbar = (ScrollBar)property.GetValue(dataGridView1, null);
        scrollbar.Visible = true;
        scrollbar.VisibleChanged += new EventHandler(ScrollBar_VisibleChanged);
    }
}

void ScrollBar_VisibleChanged(object sender, EventArgs e)
{
    FieldInfo field = dataGridView1.GetType().GetField(
        "layout", BindingFlags.NonPublic | BindingFlags.Instance);
    if (field != null)
    {
        object layoutData = field.GetValue(dataGridView1);
        FieldInfo insideField = layoutData.GetType().GetField(
            "Inside", BindingFlags.Public | BindingFlags.Instance);
        Rectangle rect = (Rectangle)insideField.GetValue(layoutData); 

        ScrollBar scrollBar = (ScrollBar)sender;
        scrollBar.Visible = true;
        scrollBar.SetBounds(
            rect.Left, rect.Height - scrollBar.Height + 1,
            rect.Width, scrollBar.Height);
    }
}

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文