调整DataGridView列时显示工具提示

时间:2011-11-17 14:31:33

标签: c# winforms datagridview tooltip

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

这么简单吗?

2 个答案:

答案 0 :(得分:1)

我希望此链接可以帮助您......如何在调整datagridview列的大小时显示工具提示...

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

或者你可以试试这个..

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

答案 1 :(得分:0)

我目前的解决方案,基于@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;
    }