DataGridView最右边的列用户增加大小不起作用

我在WinForm上的DataGridView有一个奇怪的问题。 我可以通过鼠标调整所有列的大小,但对于最右边我只能收缩而不是增加大小。

有谁知道如何解决这个问题?

这是设计的。 单击/按住鼠标按钮时,鼠标将被捕获,无法离开客户区,从而阻止resize。 你必须推动它。

我试着不要P / Invoke来发布捕获。
看看这是否适合您(当然,如果设置了任何AutoSize模式,则不会发生任何事情)。

private bool IsLastColumn = false; private bool IsMouseDown = false; private int MouseLocationX = 0; private void dataGridView1_MouseDown(object sender, MouseEventArgs e) { int _LastColumnIndex = dataGridView1.Columns.Count - 1; //Check if the mousedown is contained in last column boundaries //In the middle of it because clicking the divider of the row before //the last may be seen as inside the last too. Point _Location = new Point(eX - (dataGridView1.Columns[_LastColumnIndex].Width / 2), eY); if (dataGridView1.GetColumnDisplayRectangle(_LastColumnIndex, true).Contains(_Location)) { //Store a positive checks and the current mouse position this.IsLastColumn = true; this.IsMouseDown = true; this.MouseLocationX = e.Location.X; } } private void dataGridView1_MouseMove(object sender, MouseEventArgs e) { //If it's the last column and the left mouse button is pressed... if ((this.IsLastColumn == true) && (this.IsMouseDown == true) && (e.Button == System.Windows.Forms.MouseButtons.Left)) { // Adding the width of the vertical scrollbar, if any. int cursorXPosition = eX; if (dataGridView1.Controls.OfType().Where(s => s.Visible).Count() > 0) { cursorXPosition += SystemInformation.VerticalScrollBarWidth; } //... calculate the offset of the movement. //You'll have to play with it a bit, though int _ColumnWidth = dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width; int _ColumnWidthOffset = _ColumnWidth + (eX - this.MouseLocationX) > 0 ? (eX - this.MouseLocationX) : 1; //If mouse pointer reaches the limit of the clientarea... if ((_ColumnWidthOffset > -1) && (cursorXPosition >= dataGridView1.ClientSize.Width - 1)) { //...resize the column and move the scrollbar offset dataGridView1.HorizontalScrollingOffset = dataGridView1.ClientSize.Width + _ColumnWidth + _ColumnWidthOffset; dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width = _ColumnWidth + _ColumnWidthOffset; } } } private void dataGridView1_MouseUp(object sender, MouseEventArgs e) { this.IsMouseDown = false; this.IsLastColumn = false; }