DataGridView hiding Column Headers when parent is Docked

102 Views Asked by At

I have a Panel that includes a DataGridView. I need the Panel and (in turn) the DataGridView to fill the top 1/2 of the Form. The issue comes when, trying to do so, the DGV hides the column headers.

Panel:

{
    public DgvAccounts dgvAccounts;
    public SidePanel sidePanel;
    public TopPanel()
    {
            this.dgvAccounts = new DgvAccounts();
            this.sidePanel = new SidePanel();

            this.SuspendLayout();
            this.AutoScroll = true;
            this.Controls.Add((Control)this.dgvAccounts);
            this.Controls.Add((Control)this.sidePanel);
            this.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            this.Dock = DockStyle.Fill;
            this.Location = new Point(0, 24);
            this.Name = "TopPanel";
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowOnly;
            this.TabIndex = 21;
            this.ResumeLayout(false);
            this.PerformLayout();
    }
}

DgvAccounts:

public class DgvAccounts : DataGridView
{
    public DgvAccounts()
    {
            DataGridViewCellStyle dgvcSyle = new DataGridViewCellStyle();
            dgvcSyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvcSyle.BackColor = SystemColors.Control;
            dgvcSyle.Font = new Font("Segoe UI", 8.25f, FontStyle.Regular, GraphicsUnit.Point, (byte)0);
            dgvcSyle.ForeColor = SystemColors.WindowText;
            dgvcSyle.SelectionBackColor = SystemColors.Highlight;
            dgvcSyle.SelectionForeColor = SystemColors.HighlightText;
            dgvcSyle.WrapMode = DataGridViewTriState.True;


            DataGridViewCellStyle dgvcSyleMid = new DataGridViewCellStyle();
            dgvcSyleMid.Alignment = DataGridViewContentAlignment.MiddleCenter;

            ((ISupportInitialize)this).BeginInit();
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.AllowUserToOrderColumns = true;
            this.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            this.ColumnHeadersDefaultCellStyle = dgvcSyle;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.ColumnHeadersVisible = true;
            this.Columns.AddRange((DataGridViewColumn)this.colCheckBox, (DataGridViewColumn)this.Account,...);
            this.Location = new Point(5, 3);
            this.Name = "dgvAccounts";
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.AutoSize = true;
            this.SetAutoSizeMode(AutoSizeMode.GrowOnly);
            this.TabIndex = 1;
            this.AllowUserToResizeRows = false;
            this.RowHeadersVisible = false;
            ((ISupportInitialize)this).EndInit();


            this.colCheckBox.DataPropertyName = "colCheckBox";
            this.colCheckBox.HeaderText = "Select";
            this.colCheckBox.Name = "colCheckBox";
            this.colCheckBox.Resizable = DataGridViewTriState.False;
            this.colCheckBox.Width = 50;

            this.Account.DataPropertyName = "Account";
            this.Account.DefaultCellStyle = dgvcSyleMid;
            this.Account.HeaderText = "Account";
            this.Account.Name = "Account";
            this.Account.ReadOnly = true;
    }
}

How can I get Panel and DgvAccounts to stretch the entire width of the overall form, but still have the DgvAccounts display the Headers field? (This would have to allow for the SidePanel to resize the DgvAccounts when the SidePanel is made visible.)

View without Panel being DockStyle.Fill: enter image description here

View with Panel being DockStyle.Fill:enter image description here

0

There are 0 best solutions below