简体   繁体   English

如何在 c# 的面板滚动条中使用 datagridview 滚动条功能?

[英]How can i use datagridview scrollbar functionality in panel scrollbar in c#?

In my WinForm project I have panel control and it has DataGridview child.在我的 WinForm 项目中,我有面板控件,它有 DataGridview 子项。 Datagridview has 50 columns and this needs scrolling. Datagridview 有 50 列,这需要滚动。 but i don't want to use Datagridview scroll bar.但我不想使用 Datagridview 滚动条。 I want to use panel scroll bar.我想使用面板滚动条。 But these two scrollbar has different functionalities.但这两个滚动条具有不同的功能。 Datagridview scrollbar has more capabilities than panel scroll bar. Datagridview 滚动条比面板滚动条有更多的功能。

For example,例如,

1)With DatagirdView scrollbar you can freeze columns easily. 1) 使用 DatagirdView 滚动条,您可以轻松冻结列。

2)Also while entering data in datagridview, with TAB key, scrollbars move automatically. 2)同样在datagridview中输入数据时,使用TAB键,滚动条会自动移动。

Is there any solution to add Datagridview scroll bar functions to panel scrollbar's.是否有任何解决方案可以将 Datagridview 滚动条功能添加到面板滚动条。

Or How to add the above 2 functions to panel scrollbar.或者如何将以上 2 个功能添加到面板滚动条。

Thanx in advance.提前谢谢。

The panel scrollbar can only be used to control the position of the entire datagridview.面板滚动条只能用来控制整个datagridview的position。

Here is a workaround that using external Scrollbar to scroll datagridview.这是使用外部滚动条滚动数据网格视图的解决方法。

First, need to add a HScrollBar to the form and set its properties as followed.首先,需要在表单中添加一个 HScrollBar 并设置它的属性如下。

hScrollBar1.Maximum = dataGridView1.Columns[0].Width * dataGridView1.ColumnCount + dataGridView1.RowHeadersWidth;
hScrollBar1.Value = 0;
hScrollBar1.Location = new Point(panel1.Location.X, panel1.Location.Y + panel1.Height);

And subscribe to Scroll event并订阅Scroll事件

hScrollBar1.Scroll += hScrollBar_Scroll;
private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    int rowindex = dataGridView1.CurrentCell.RowIndex;
    int columnindex = (hScrollBar1.Value - dataGridView1.RowHeadersWidth) / dataGridView1.Columns[0].Width;
    this.dataGridView1.CurrentCell = this.dataGridView1[columnindex, rowindex];
}

If want to change the scrollbar value according to the selected cell, subscribe to DataGridView.SelectionChanged event.如果要根据所选单元格更改滚动条值,请订阅DataGridView.SelectionChanged事件。

dataGridView1.SelectionChanged += dataGridView_SelectionChanged;
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    hScrollBar1.Value = (dataGridView1.CurrentCell.ColumnIndex + 1) * dataGridView1.Columns[0].Width + dataGridView1.RowHeadersWidth;
}

The above columnindex and hScrollBar1.Value are obtained according to the following formula:上面的columnindexhScrollBar1.Value是根据下面的公式得到的:

在此处输入图像描述

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

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