子类化的DataGridView的列宽问题——鼠标不能该表列宽

使用了子类化的DataGridView类作为基类,也使用了变成排序方式,然后就发现列宽不能响应鼠标的拖拽了。

发现以下资料,还有待尝试。

1.响应ColumnWidthChanged事件

2.响应鼠标按下事件,解决哪个列被更改的事实。

private void dataGridView_MouseDown(object sender, MouseEventArgs e)
{
    for (int i = 0; i < dataGridView.ColumnCount; i++)
    {
        if (dataGridView.GetColumnDisplayRectangle(i, true).Contains(e.Location))
        {
            columnBeingResized = dataGridView.Columns[i];
            break;
        }
    }
}
  3.发现.net4.5支持 ColumnDividerWidthChanged事件,回去查下.net4支持不?
private void DataGridView1_ColumnDividerWidthChanged(Object sender, DataGridViewColumnEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "Column", e.Column );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "ColumnDividerWidthChanged Event" );
}
4.有人说需要修改mouse-entering-in-cell事件,然后编程改变光标为 resize光标,并且更改列宽?

可能地这样解决…

// the OnCellMouseDown is overriden so the control can check to see if the
// user clicked the + or – sign of the group-row
protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
// Old code was: if (e.RowIndex < 0) return;
// This obviously shorted out the mouse events and prevented the standard grid
// from handling column resizing
if (e.RowIndex < 0)
base.OnCellMouseDown(e);
else
{
OutlookGridRow row = (OutlookGridRow)base.Rows[e.RowIndex];
if (row.IsGroupRow && row.IsIconHit(e))
{
System.Diagnostics.Debug.WriteLine(“OnCellMouseDown ” + DateTime.Now.Ticks.ToString());
row.Group.Collapsed = !row.Group.Collapsed;

//this is a workaround to make the grid re-calculate it’s contents and backgroun bounds
// so the background is updated correctly.
// this will also invalidate the control, so it will redraw itself
row.Visible = false;
row.Visible = true;
}
else
base.OnCellMouseDown(e);
}
}

0 Comments

  1. codeex

    最后一种方式搞定!!!!!

    Reply

Leave a Comment

电子邮件地址不会被公开。 必填项已用*标注