You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现DataGridView行列调整的实时平滑显示?

DataGridView行/列实时调整实现方案

要解决DataGridView调整行/列时需等待操作结束才更新、无实时调整条的问题,可通过自定义控件重写鼠标事件实现实时更新,具体步骤如下:

1. 自定义DataGridView控件

创建继承自DataGridView的自定义控件,重写鼠标相关事件和绘制方法,手动处理调整过程中的尺寸更新与调整条绘制。

2. 核心代码实现

public class LiveResizeDataGridView : DataGridView
{
    private bool _isResizingColumn;
    private bool _isResizingRow;
    private int _resizeIndex;
    private int _startPos;
    private int _originalSize;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        var hitTest = HitTest(e.X, e.Y);
        // 判定是否触发列调整
        if (hitTest.Type == DataGridViewHitTestType.ColumnResizeLeft || hitTest.Type == DataGridViewHitTestType.ColumnResizeRight)
        {
            _isResizingColumn = true;
            _resizeIndex = hitTest.ColumnIndex;
            _startPos = e.X;
            _originalSize = Columns[_resizeIndex].Width;
        }
        // 判定是否触发行调整
        else if (hitTest.Type == DataGridViewHitTestType.RowResizeTop || hitTest.Type == DataGridViewHitTestType.RowResizeBottom)
        {
            _isResizingRow = true;
            _resizeIndex = hitTest.RowIndex;
            _startPos = e.Y;
            _originalSize = Rows[_resizeIndex].Height;
        }
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if (_isResizingColumn)
        {
            int delta = e.X - _startPos;
            int newWidth = _originalSize + delta;
            // 限制列最小宽度,避免过窄
            if (newWidth >= MinimumColumnWidth)
            {
                Columns[_resizeIndex].Width = newWidth;
                Invalidate();
            }
        }
        else if (_isResizingRow)
        {
            int delta = e.Y - _startPos;
            int newHeight = _originalSize + delta;
            // 限制行最小高度
            if (newHeight >= MinimumRowHeight)
            {
                Rows[_resizeIndex].Height = newHeight;
                Invalidate();
            }
        }
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        _isResizingColumn = false;
        _isResizingRow = false;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // 绘制实时调整条,模拟系统原生样式
        if (_isResizingColumn)
        {
            int colEdge = Columns[_resizeIndex].Left + Columns[_resizeIndex].Width;
            using (Pen darkPen = new Pen(SystemColors.ControlDark))
            {
                e.Graphics.DrawLine(darkPen, colEdge, 0, colEdge, Height);
            }
            using (Pen lightPen = new Pen(SystemColors.ControlLightLight))
            {
                e.Graphics.DrawLine(lightPen, colEdge + 1, 0, colEdge + 1, Height);
            }
        }
        else if (_isResizingRow)
        {
            int rowEdge = Rows[_resizeIndex].Top + Rows[_resizeIndex].Height;
            using (Pen darkPen = new Pen(SystemColors.ControlDark))
            {
                e.Graphics.DrawLine(darkPen, 0, rowEdge, Width, rowEdge);
            }
            using (Pen lightPen = new Pen(SystemColors.ControlLightLight))
            {
                e.Graphics.DrawLine(lightPen, 0, rowEdge + 1, Width, rowEdge + 1);
            }
        }
    }
}

3. 使用说明

  • 将项目中原有的DataGridView控件替换为自定义的LiveResizeDataGridView
  • 可根据业务需求修改MinimumColumnWidth和MinimumRowHeight的限制值
  • 若需要更贴合系统原生的调整条样式,可优化OnPaint方法中的绘制逻辑

内容的提问来源于stack exchange,提问作者CocoaMix86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:35:05