DataGridView最右边的列用户增加大小无法正常工作 [英] DataGridView rightmost column user increase size not working
本文介绍了DataGridView最右边的列用户增加大小无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WinForm上的DataGridView有一个奇怪的问题。我可以通过鼠标调整所有列的大小,但最右边的内容只能缩小,而不能增加大小。
I have a strange issue with a DataGridView on a WinForm. I can resize all columns via mouse, but for the rightmost I can only shrink and not increase the size.
有人知道如何解决这个问题?
Does anyone know how to solve this?
推荐答案
这是设计使然。单击/按住鼠标按钮时,鼠标会被捕获并且无法离开客户区,从而阻止了调整大小。
This is by design. When you Click/Hold your mouse button, the mouse is captured and cannot leave the client area, with the effect of blocking the resize. You have to give it a push.
我尝试不进行P / Invoke释放捕获。
看看是否适用
I tried not to P/Invoke to release the capture.
See if this works for you (of course if any AutoSize mode is set, nothing will happen).
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(e.X - (dataGridView1.Columns[_LastColumnIndex].Width / 2), e.Y);
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 = e.X;
if (dataGridView1.Controls.OfType<VScrollBar>().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 + (e.X - this.MouseLocationX) > 0 ? (e.X - 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;
}
这篇关于DataGridView最右边的列用户增加大小无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文