Changing width of vertical scroll bar in dataGridV

2019-02-25 14:19发布

问题:

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. 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

 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.

Next I put this into where I build the MainForm still no good the vertical scroll bar still looks the same.

2) I find that I could add a scroll bar from the tool box. A bit of progress here until I try to connect to 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.

回答1:

You can turn off the DGV's vertical scroll bar:

dataGridView1.ScrollBars = ScrollBars.Horizontal;

And add a VerticalScrolllBar Control instead. Make sure to keep its size in snych and also its Maximum:

vScrollBar1.Maximum = dataGridView1.RowCount;

To scroll in synch code both Scroll events:

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


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