【发布时间】:2019-10-10 14:31:29
【问题描述】:
我在将 Hscroll 绑定到 datagridview 时遇到问题。我需要比默认的 DGV 更大的 scoll。所以我需要将自定义滚动绑定到 dgv 或增加默认滚动的高度。我正在使用 WinForms。
我尝试了以下代码,但它不符合我的需要,滚动停止在我的 dgv 中间,操作值对其没有影响。
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
int totalwidth = dataGridView1.RowHeadersWidth;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
totalwidth += dataGridView1.Columns[i].Width ;
}
hScrollBar1.LargeChange = dataGridView1.Width;
hScrollBar1.SmallChange = dataGridView1.Columns[gsKodTowaruDataGridViewTextBoxColumn.Index].Width;
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
hScrollBar1.Value = e.NewValue;
}
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView1.HorizontalScrollingOffset = e.NewValue;
}
感谢您的回答。
【问题讨论】:
-
如何设置最大值?也许
totalwidth?? -
是的,这种方式不能正常工作
-
a)“不正确”和 b)“是”是什么意思。如果您没有正确设置最大值,滚动条显然无法工作。每当添加或调整列大小时,您还需要对其进行调整。显示代码!
-
a) - 没有一直移动,大约是“默认”滚动条长度的 50%。 ---- hScrollBar1.Maximum = 总宽度; hScrollBar1.LargeChange = dataGridView1.Width+50; hScrollBar1.SmallChange = dataGridView1.Columns[gsKodTowaruDataGridViewTextBoxColumn.Index].Width;在for循环之后它现在看起来像这样^^
-
一个简单的解决方案是与 dgv 的 hscrollbar 同步:
hScrollBar1.Maximum = totalwidth; HScrollBar vBar = dataGridView1.Controls.OfType<HScrollBar>().First(); hScrollBar1.LargeChange = vBar != null ? vBar.LargeChange : totalwidth / 4;
标签: c# winforms datagridview horizontal-scrolling