简体   繁体   English

在dataGridView中更改垂直滚动条的宽度

[英]Changing width of vertical scroll bar in dataGridView

I am developing an App for a touchscreen. 我正在为触摸屏开发应用程序。 I have been asked to make the size of the scroll bars bigger so the users can use them. 我被要求使滚动条的大小更大,以便用户可以使用它们。 So far I have not been able to get this sorted. 到目前为止,我还没有能够对此进行排序。 I read that if you increase the width of MainForm window scroll bar then dataGridView will inherit it. 我读到如果你增加MainForm窗口滚动条的宽度,那么dataGridView将继承它。 I have tried a few things but so far have failed to get it to work. 我尝试过一些东西,但到目前为止还没有让它发挥作用。

The two closest ways I tried are 我尝试的两种最接近的方式是

1) When I build the grid I add the following 1)当我构建网格时,我添加以下内容

 foreach (Control ctrl in dataGridView1.Controls)
    if (ctrl.GetType() == typeof(VScrollBar))
       ctrl.Width = 86;

Unfortunately this seems to get the Width of 17 but not able to override it with this new value of 86. 不幸的是,这似乎得到宽度为17但不能用这个86的新值覆盖它。

Next I put this into where I build the MainForm still no good the vertical scroll bar still looks the same. 接下来我把它放到我构建MainForm的地方仍然没有好的垂直滚动条看起来仍然相同。

2) I find that I could add a scroll bar from the tool box. 2)我发现我可以从工具箱中添加滚动条。 A bit of progress here until I try to connect to dataGridView. 这里有一点进展,直到我尝试连接到dataGridView。 This I cannot do. 这个我做不到。 I have an event so everytime it is moved I should be able to move the grid. 我有一个事件所以每次移动我都应该能够移动网格。 Below commented out are a few items that I use to make sure I am getting a value. 下面注释了我用来确保获得价值的一些项目。

 private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        //MessageBox.Show(vScrollBar1.Value.ToString());
       // MessageBox.Show(SystemInformation.VerticalScrollBarWidth.ToString());
      //  CalculateVerticalScrollbarWidth() * 4;
    }

So I thought I would ask the audience of higher intelligence than me as someone may have solved this and will share the answer with me. 所以我想我会问比我更高智商的观众,因为有人可能已经解决了这个问题,并会与我分享答案。

You can turn off the DGV 's vertical scroll bar: 您可以关闭DGV的垂直滚动条:

dataGridView1.ScrollBars = ScrollBars.Horizontal;

And add a VerticalScrolllBar Control instead. 并添加VerticalScrolllBar控件。 Make sure to keep its size in snych and also its Maximum : 确保将其大小保持在snych及其Maximum

vScrollBar1.Maximum = dataGridView1.RowCount;

To scroll in synch code both Scroll events: 要在同步码的两种滚动Scroll事件:

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
    vScrollBar1.Value = e.NewValue;
}


private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
   dataGridView1.FirstDisplayedScrollingRowIndex = e.NewValue;
}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM