当DataGridView列被调整大小时显示工具提示

本文关键字:小时 显示 工具提示 调整 DataGridView | 更新日期: 2023-09-27 18:14:53

当用户在WinForms DataGridView中调整列的大小时,我试图对Excel做类似的效果,其中工具提示显示列的当前大小。

有简单的方法吗?

当DataGridView列被调整大小时显示工具提示

希望这个链接能帮到你....关于如何在调整datagridview列的大小时显示工具提示…

http://www.codeproject.com/KB/grid/testgridzip.aspx?display=PrintAll

或者can you try this.

 void dataGridView1_ColumnWidthChanged(object sender,  DataGridViewColumnEventArgs e)
   {
       toolTip1.SetToolTip(dataGridView1.Columns[i].Width.ToString());
    }

我目前的解决方案,基于@errorstacks提示。

        this.dgvDeskTrades.MouseDown += dgvDeskTrades_MouseDown;
        this.dgvDeskTrades.MouseMove += dgvDeskTrades_MouseMove;
        this.dgvDeskTrades.MouseUp += dgvDeskTrades_MouseUp;
    private int colResizing = -1;
    private int origWidth;
    private int mouseX;
    private void dgvDeskTrades_MouseDown(object sender, MouseEventArgs e)
    {
        var hit = this.dgvDeskTrades.HitTest(e.X, e.Y);
        if (hit.Type != DataGridViewHitTestType.ColumnHeader) return;
        if (Cursor.Current != Cursors.SizeWE) return;
        this.colResizing = hit.ColumnIndex;
        this.origWidth = this.dgvDeskTrades.Columns[this.colResizing].Width;
        this.mouseX = e.X;
    }
    private void dgvDeskTrades_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.colResizing == -1) return;
        this.dgvDeskTrades.Columns[this.colResizing].Width = Math.Max(0, this.origWidth + (e.X - this.mouseX));
        this.toolTip.Show("Width: " + this.dgvDeskTrades.Columns[this.colResizing].Width, this, e.X, e.Y);
    }
    private void dgvDeskTrades_MouseUp(object sender, MouseEventArgs e)
    {
        if (this.colResizing == -1) return;
        this.toolTip.Hide(this);
        this.colResizing = -1;
    }