0

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

  • 2
    The Panel is probably docking under the MenuStrip. Add the MenuStrip to its own container. You can also try `[TopPanel].BringToFront()` before docking it. But you should use different containers to better control the docking hierarchy anyway. – Jimi Sep 08 '21 at 14:39
  • Yup that was it. The MenuStrip was hiding it. I changed the Controls to load the MenuStrip last and now it shows up correctly. Thank you much! – Robert Kelley Sep 08 '21 at 20:30

0 Answers0