Reputation: 295
I'm displaying a DataGridView
object in the application, but a part of it is hidden behind a MenuStrip
(in the same application) which is at the top of the screen.
I don't know if that matters but the MenuStrip
is created with the Visual Studio designer, while the DataGridVie
w is created programmatically.
I set the DataGridView
's object to DockStyle.Fill
, I have tried other styles and I have also tried to useAnchorStyle
, neither worked.
The part that is hidden is pretty much the column headers part.
Is there a way to fix this? Maybe set it somehow relative to the MenuStrip
?
Upvotes: 0
Views: 2069
Reputation: 818
Use the Anchor property instead of the dock property. Something like this should work:
grid.Top = menuStrip1.Height;
grid.Height = this.ClientSize.Height - menuStrip1.Height; //this => parent form
grid.Width = this.ClientSize.Width;
grid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
EDIT: Note that if the DockProperty is set to anything else than None this will override the Anchor property
Edit: Updated Typing Mistake
Upvotes: 3
Reputation: 54433
First check the current Parent by putting this in the Form's Load or Shown event:
Console.WriteLine( menuStrip1.Parent.Text + "<---menuStrip1.Parent---");
If the MenuStrip
is correctly docked you should see your Form's title text in the output..
If it isn't you can log out the menuStrip1.Parent.Name
. And maybe it is enough to set the parent to be the form before docking the DataGridView
:
menuStrip1.Parent = this;
But if that helps you really ought to fix the initial error:
Remove the MenuStrip
(cut), make sure there is free room for it at the top of the Form
and add it again (paste). If they don't have proper space to dock to the top of the window MenuStrips
tend to dock somewhere else and then mess up the layout.
Upvotes: 0
Reputation: 81610
The order of controls getting added to the form are affecting the layout when you use a property like Dock. You can simple use the BringToFront method to fix the issue:
MenuStrip ms = new MenuStrip();
ms.Items.Add("File");
this.Controls.Add(ms);
DataGridView dgv = new DataGridView();
dgv.Columns.Add("Test", "Test");
dgv.Dock = DockStyle.Fill;
this.Controls.Add(dgv);
dgv.BringToFront();
Upvotes: 1