Я расширил DataGridView
, но, к сожалению, после использования шаблона из Toolbox он генерирует параметры по умолчанию для меня. Похоже, это отменяет мои настройки. Что я здесь не так делаю?
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;
}
}
}