Reputation: 95
I have a tabControl with two tabs. I need to change window size when I click on tab2, by width of dataGrid which is in second tab. I have the following code:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])
{
//FormValue.form2.Width = magical_function_return_dataGridwidth();
txtNumber.Select();
}
else
{
FormValue.form2.Width = 580;
}
}
Or is there some more elegant way. I was playing with properties of dataGrid ... auto resize windows, but I found nothing ...
In dataGrid I use AutoSizeColumnMode: AllCells, because I want resize cell size by text string length in it and I want to achieve the same behaviour for window.
Thank you.
Upvotes: 0
Views: 759
Reputation: 2469
I had a go at implementing your magical_function_return_dataGridwidth
function.
The width you are looking for comes from the RowHeadersWidth
, the DataGridViewColumn
objects, the Scrollbar (if it's visible) and the general padding / borders etc.
I used a nasty constant of 50 pixels to deal with all of the bits that I couldn't work out, but for me it works correctly and deals with the presence of the vertical scrollbar.
It's not an ideal solution by any means, but hopefully it will help you.
private int magical_function_return_dataGridwidth(DataGridView dgv)
{
int totalWidth = dgv.RowHeadersWidth + 50;
foreach (var scroll in dgv.Controls.OfType<VScrollBar>())
{
if(!scroll.Visible)
totalWidth -= System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
}
foreach (DataGridViewColumn col in dgv.Columns)
totalWidth += col.Width;
return totalWidth;
}
Upvotes: 1