Reputation: 43
Can we display multiple DataGridView in a TableLayoutPanel? If yes, how can we achieve that? I searched on msdn and also googled it but can't find anything with my requirement.
I can display a label multiple times dynamically but I am looking for DataGridView dynamically.
Any help would be appreciated. Thanks!
Upvotes: 0
Views: 942
Reputation: 4835
It's not clear how you want the child controls to be displayed. You mentioned that you can do it with a Label
, but you aren't showing any of the code, which would help clarify. It is exactly the same for a DataGridView
.
As you've noticed you can only put a single Control in a TableLayoutPanel
"cell". If you want multiple controls in a cell, you can add a Panel
first to the TableLayoutPanel
, and then add multiple child controls into that Panel
.
However, I would assume that what you really want is a variable number of columns or rows in your TableLayoutPanel
, and then add one DataGridView
per cell.
First, create a TableLayoutPanel
or acquire an existing instance.
// If you are using an existing table layout panel, either clear the controls, rows and columns beforehand,
// or keep track of them manually and adjust accordingly.
tableLayoutPanel.Controls.Clear();
tableLayoutPanel.RowStyles.Clear();
tableLayoutPanel.ColumnStyles.Clear();
IList<DataGridView> dataGridViews = ... /* wherever you get your data grid views from */
for (int i = 0; i < dataGridViews.Count; ++i)
{
// The arguments to ColumnStyle control the size of the column.
// If you want to arrange them vertically instead of horizontally, use RowStyles instead.
// If you want a combination, you have to figure out the logic yourself.
// In case of SizeType.Percent, "width" defines a relative weight, not necessarily percent.
// If all widths are equal (no matter the value), all columns will be equally wide.
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, width: 1));
dataGridViews[i].Dock = DockStyle.Fill;
// This adds the data grid view into that specific cell.
tableLayoutPanel.Controls.Add(dataGridViews[i], column: i, row: 0);
}
Upvotes: 1
Reputation: 34421
Try following
public partial class Form1 : Form
{
const int WIDTH = 1200;
const int HEIGHT = 1200;
const int MARGIN = 200;
const int NUMBER_OF_ROWS = 2;
const int NUMBER_OF_COLS = 2;
static List<DataGridView> dgvs = new List<DataGridView>();
public Form1()
{
InitializeComponent();
this.Width = WIDTH;
this.Height = HEIGHT;
TableLayoutPanel panel = new TableLayoutPanel();
this.Controls.Add(panel);
panel.Height = HEIGHT - MARGIN;
panel.Width = WIDTH - MARGIN;
panel.ColumnCount = NUMBER_OF_COLS;
panel.RowCount = NUMBER_OF_ROWS;
for (int row = 0; row < NUMBER_OF_ROWS; row++)
{
for (int col = 0; col < NUMBER_OF_COLS; col++)
{
DataGridView newDGV = new DataGridView();
dgvs.Add(newDGV);
newDGV.Height = (HEIGHT - MARGIN) / NUMBER_OF_ROWS;
newDGV.Width = (WIDTH - MARGIN) / NUMBER_OF_COLS;
newDGV.Left = 50;
newDGV.Top = 50;
panel.Controls.Add(newDGV, col, row);
}
}
}
Upvotes: 1