可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've encountered a bug (I assume) in .NET 3.5. When adding rows to a DataGridView using Rows.Add(), while the DGV is disabled, the vertical scrollbar doesn't update properly. Consequently you can't scroll all the way to the bottom of the DGV using the scrollbar or the mouse wheel after reenabling the DGV (navigating with arrow keys still works, though.)
So I'm looking for a workaround. Is there a way to force the scrollbar to update its bounds or can you manually input a new maximum value? I'd rather not have to repopulate the DGV.
*) Actually, it's the parent form that's disabled, but I assume the problem is that it propagates to the DGV control.
回答1:
I've just had this problem (my form was disabled while adding rows) and solved it by setting the scrollbar property of the grid to 'None' before adding the rows then setting it back to 'Both' once all my rows have been added.
回答2:
This also solved the problem for me:
DataGridView.SuspendLayout();
DataGridView.ResumeLayout();
It can be called before the DGV is re-enabled.
UPDATE:
This also does the job:
DataGridView.PerformLayout();
回答3:
My problem was that my vertical scrollbar disappeared completely. I flailed with all of the above suggestions and finally discovered that making the panel containing the DataGridView narrower than the form solved the problem. In my case, 16 pixels narrower worked.
回答4:
If none of the other given solution worked for you, I came across a similar issue with vertical scrollbar in DataGridView. But the issue is like whenever the number of rows extend beyond the height of the datagridview, vertical scrolling created a messed up UI. Kind of rows overlapping each other.
I had a databound DataGridView.
These are the list of things I tried but didn't work.
- Setting the ScrollBars property to None, modify datasource and then set the ScrollBars property to Both.
- Using SuspendLayout, ResumeLayout and PerformLayout at various combinations.
- Set Double Buffering for the DataGridView using extension method.
Finally, Setting AutoSizeRowsMode to DataGridViewAutoSizeRowsMode.AllCells fixed the issue for me.
If you have similar issue with horizontal scrolling, I think playing with AutoSizeColumnsMode should fix the issue.
回答5:
Actually, I just found one workaround but I don't like it. After the DGV is reenabled you can do this:
int x = Rows.Add();
Rows.RemoveAt(x);
And then the scrollbar is updated. But it's not very pretty, it causes an annoying little flicker, and it might fire some events which I'd have to deliberately ignore. I'll leave the question open for a bit in the hope of a better solution.
回答6:
It was observed that, when the DataGridView1's width and height were compared with the width and height of the form, and the width and height were reset if they exceeded the form's dimensions, the scroll bars became visible.
Try the following code, which will dynamically add a DataGridView control to a Form and create a square grid with row and column header names:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Following code adds a Datagridview control to a Form dynamically
'Step 1. Add a textbox to a Form, and input the number of columns (ncol). (Note: in this example, ncol=nrow).
'Step 2. Set the Form's Windowstate property to Maximized
For Each cont As Control In Me.Controls 'remove DataGridView if it already exists on the Form
If TypeOf (cont) Is DataGridView Then
Me.Controls.Remove(cont)
End If
Next
Dim DataGridView1 As New DataGridView 'create new data grid view dynamically during run-time
Me.Controls.Add(DataGridView1) 'add the data grid view to the Form
Me.Refresh()
Dim i, nrow, ncol As Integer ' ncol=nrow -->this is a square grid
ncol = TextBox1.Text
nrow = ncol 'Note: add a second textbox to the form and input nrow if you don't want a square grid
DataGridView1.Visible = True
DataGridView1.Top = 100
DataGridView1.Left = 100
DataGridView1.Rows.Clear()
Do While DataGridView1.Columns.Count > 0
DataGridView1.Columns.RemoveAt(DataGridView1.Columns.Count - 1)
Loop
For i = 1 To ncol
DataGridView1.Columns.Add(i, "V" & i)
Next
DataGridView1.Width = ncol * 115
DataGridView1.Height = nrow * 22 + 45
If DataGridView1.Width > Me.Width - DataGridView1.Left Then DataGridView1.Width = Me.Width - DataGridView1.Left - 20
If DataGridView1.Height > Me.Height - DataGridView1.Top Then DataGridView1.Height = Me.Height - DataGridView1.Top - 50
DataGridView1.ScrollBars = ScrollBars.None
For i = 1 To nrow
DataGridView1.Rows.Add()
DataGridView1.Rows.Item(i - 1).HeaderCell.Value = "V" & i
Next
DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
DataGridView1.AllowUserToAddRows = False
DataGridView1.ScrollBars = ScrollBars.Both
Me.WindowState = FormWindowState.Maximized
End Sub
回答7:
I would like to add a comment to the original post, but I can't yet (lower than 50 reputation).
I have encountered the same problem on deleting rows. The scrollbar looks like disabled, no slider is visible and the arrows are grey.
Will try the workarounds described here and at this link (explicitly enable the scrollbars again) or simply keep the whole DGV enabled.
Also, this link suggests the same workaround (explicitly enabling...) and calls it working.
回答8:
My problem stemmed from calling dgv.Add() in a user thread. After changing it to be called from the UI thread instead, the scroll bar displayed and functioned normally:
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke((Action)(() => dataGridView1.Rows.Add(new String[] { abc, efg })));
}
else
{
dataGridView1.Rows.Add(new String[] { calPoint, logUrl });
}
回答9:
As the slider was not sizing correctly and took up most of the vertical scrollbar my solution was -
DGV.height = DGV.Height + 1
DGV.Height = DGV.Height - 1
Then the slider was correctly sized
But I now use
DGV.PerformLayout
which also solves the problem
回答10:
I found your post while searching for a fix for the issue I was having. What I encountered on my Microsoft Surface (Win10) was the inability to vertical scroll the DataGridView to the very last line of a long list using a touch gesture (like flick). Frequently, the last line was maddeningly hard to get to. The solution was simple but took me a while to figure out. I'm leaving it here in case it's helpful.
// Override WndProc in your custom class inherited from DataGridView
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x115:// WM_VSCROLL
// The low-order word holds the command
uint cmd = ((uint)m.WParam & (uint)0x0000FFFF);
switch (cmd)
{
case 5: // SB_THUMBTRACK
if (Rows.Count > 0)
{
// The high-order word holds the position
uint pos = ((uint)m.WParam & (uint)0xFFFF0000) >> 16;
// SAVE: This would give us the "true" ratio based on 100%
// SAVE: double ratio = (double)pos / (double)(VerticalScrollBar.Maximum - VerticalScrollBar.LargeChange);
// SAVE: Debug.WriteLine("Scroll Position: " + pos + "\t" + (ratio * 100.0).ToString("F2") + "%");
// What we want is the ratio to the TOP of the thumb, BECAUSE
// THIS GIVES US THE RATIO TO THE FIRST LINE INDEX
double firstLineRatio = (double)pos / (double)(VerticalScrollBar.Maximum);
// We want to make it so that it shows the full line
// even if we just barely meet the ratio
double dFirstLine = firstLineRatio * Rows.Count;
int iFirstLine = (int)(dFirstLine + 0.9999);
// SAVE: Debug.WriteLine("Scroll Position: " + pos + "\t" + (ratio * 100.0).ToString("F2") + "%");
FirstDisplayedScrollingRowIndex = iFirstLine;
// We do this INSTEAD OF the default for this message, so RETURN
return;
}
break;
}
break;
default:
break;
}
base.WndProc(ref m);
}
回答11:
My solution was to disable scrollbars from it's properties.
Then enable them from code line after initializing the window.
DataGridViewObj.ScrollBars = ScrollBars.Both
回答12:
For me the problem was that I put my datagrid in a TabPage
which was not displayed during data generation time so the srolling was messed up.
I found a was to make a correct update just by auto diable/enable at each visible change :
public MyForm()
{
InitializeComponent();
// Automatic scroll to bottom (it might not work if you try to do it without this event)
datagrid.RowsAdded += (sender, e) =>
{
datagrid.FirstDisplayedScrollingRowIndex = datagrid.RowCount - 1;
};
// WinForms bug fix: scrollbar update
datagrid.VisibleChanged += (sender, e) =>
{
Enabled = false;
Enabled = true;
};
}
回答13:
The last two rows of my DataGridView were always hidden on my WinForms. I could scroll to them using the keyboard down arrow key (but still not see which row I was actually on). The mouse wheel and scrollbar down arrow would not get to them either. Only with a small data set and maximizing the form could I see the last two rows.
Here is how I fixed the problem: I placed the DataGridView in a Panel. BAM!
It also fixed another problem with the DataGridView, that when I resized a column headers weird vertical lines would appear on any UI control below the DataGridView. It was very ugly and unprofessional looking. But now it is fixed too.