我扩展了DataGridView
,但不幸的是,在使用工具箱中的模板后,它会为我生成默认参数。看起来它覆盖了我的设置。我在这里做错什么了?
class CustomDataGrid2 : DataGridView
{
public CustomDataGrid2() : base()
{
base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
base.ColumnHeadersHeight = 23;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
base.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
base.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.ControlDark;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
base.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
base.EnableHeadersVisualStyles = false;
base.Location = new System.Drawing.Point(112, 186);
base.RowHeadersVisible = false;
base.Size = new System.Drawing.Size(401, 150);
}
}
结果:
//
// customDataGrid21
//
this.customDataGrid21.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.customDataGrid21.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.ControlDark;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.customDataGrid21.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.customDataGrid21.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.customDataGrid21.EnableHeadersVisualStyles = false;
this.customDataGrid21.Location = new System.Drawing.Point(60, 138);
this.customDataGrid21.Name = "customDataGrid1";
this.customDataGrid21.RowHeadersVisible = false;
this.customDataGrid21.Size = new System.Drawing.Size(401, 150);
this.customDataGrid21.TabIndex = 3;
如您所见,我设置了DataGridViewColumnHeadersHeightSizeMode.DisableResizing
,但它生成了AutoSize
我也尝试过:
class CustomDataGrid4 : DataGridView
{
public CustomDataGrid4() : base()
{
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
base.ColumnHeadersHeight = 23;
}
}
结果:
//
// customDataGrid41
//
this.customDataGrid41.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.customDataGrid41.Location = new System.Drawing.Point(96, 152);
this.customDataGrid41.Name = "customDataGrid41";
this.customDataGrid41.Size = new System.Drawing.Size(240, 150);
this.customDataGrid41.TabIndex = 3;
解决方案:
class CustomDataGrid6 : DataGridView
{
private DataGridViewColumnHeadersHeightSizeMode m_ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
private int m_columnHeadersHeight = 23;
public new DataGridViewColumnHeadersHeightSizeMode ColumnHeadersHeightSizeMode
{
get => this.m_ColumnHeadersHeightSizeMode;
set
{
this.m_ColumnHeadersHeightSizeMode = value;
base.ColumnHeadersHeightSizeMode = this.m_ColumnHeadersHeightSizeMode;
}
}
public int ColumnHeadersHeight
{
get => this.m_columnHeadersHeight;
set
{
this.m_columnHeadersHeight = value;
base.ColumnHeadersHeight = this.m_columnHeadersHeight;
}
}
}
答案 0 :(得分:1)
使用覆盖的OnLayout()方法设置属性:
MSDN
:派生类应重写此方法以进行任何自定义布局 逻辑。
private DataGridViewColumnHeadersHeightSizeMode m_ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
public CustomDGV() { }
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
}
public new DataGridViewColumnHeadersHeightSizeMode ColumnHeadersHeightSizeMode
{
get => this.m_ColumnHeadersHeightSizeMode;
set { this.m_ColumnHeadersHeightSizeMode = value;
base.ColumnHeadersHeightSizeMode = this.m_ColumnHeadersHeightSizeMode;
}
}
将自定义控件放到窗体上时,DataGridViewColumnHeadersHeightSizeMode.DisableResizing
将成为ColumnHeadersHeightSizeMode
的应用值
尝试使用Label.AutoSize
属性。 Autosize = false
仅在OnLayout()
方法中设置时才会在设计器中设置。