JharPaat
JharPaat

Reputation: 341

How to place two DatagridView vertically in Winform?

I would like to place two DataGridViews (Auto Binding ) vertically along with their labels in Winform. So it should fit with in area and look nice.

It should display on following order: DataGridView Label1 DataGridView1 DataGridView Label2 DataGridView2

enter image description here

It should adjust Scroll and maximize behavior automatically. I have tried with Panels and FlowLayoutPanel but was not successful. I know how to do in WPF with relative layout, but no clue how to do in winform.

Upvotes: 1

Views: 1161

Answers (2)

JharPaat
JharPaat

Reputation: 341

I solved the problem using TableLayoutPanel control. Created four rows:

Row1: SizeType:Absolute value:20

Row2:SizeType:AutoSize

Row3: SizeType:Absolute value:20 Row4:SizeType:AutoSize

And DatagridView1.AutoSize = true; DatagridView2.AutoSize = true;

tableLayoutPanel1.AutoScroll=true;

Now, it adjust Scroll and maximize behavior automatically as per different size of DataGridViewRows

Thanks LarsTech and TaW and others for your support !!

Upvotes: 0

TaW
TaW

Reputation: 54433

The problem seems to be mainly keeping the sizes of the two DGVs in synch.

For this you can use a SplitContainer, which will do that automatically:

  • Set Orientation = Orientation.Horizontal
  • Set IsSplitterFixed = true
  • Set FixedPanel to None
  • Set its Anchors to your liking, maybe to all four sides
  • Put your two DataGridViews in the two SplitContainer.Panels
  • The both DGVs to Dock=Fill

Now both DGVs will stay at the same size, sharing the SplitContainer size equally, or to be precise with the original ratio.

If you want Labels to sit above each DGV, simply put them in place but instad of Dock=Fill choose four Anchors for the DGVs.

If you want your Buttons to stay under the SplitContainer simple Anchoring will probably do..

Note that by nesting more such SplitContainers you can keep three or more Controls synched with the same sizes..

Upvotes: 2

Related Questions