Win Forms DataGridView Horizontal 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 b开发者_如何转开发ar 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
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
精彩评论