1
votes

I want to resize the tabpage, its internal Control which is dataGridview and Finally resize the form in which theay are contained.

I have implemented the Drag functionality of tabpages.Now i want to increase tabPage Size based on the DatagridviewRows.

if(dgv.Rows.count<=15)
  Resize tabPage to show  data to show 'n' No. Of Rows
else if(dgv.Rows.count>15)
  Resize to show 15 Rows data then Scroll bar.

I have tried setting the Dock and Anchor property of gridview.But only the tabpage is filled.I want the tabpage to be resized with increasing No of rows and Finally resize Form in which it is contained.

Kindly help.

3
TabPages do not get resized — they are child panels to the TabControl. The scrollbars belong to the DataGridView. Why is docking the DataGridView inside the TabPage an issue? It should be all automatic. - LarsTech
@LarsTech The grid is docked in the TabPage.But i need to resize the Tabpage.If Tabpage Or Tabcontrol is resized.Then the grid size will automatically increase.Kindly help how to do that? :(\ - lokendra jayaswal
You can try setting everything to be docked, at all levels of nesting. Then, ensure that AutoSize = true for the form and everything in it. This still might not be enough, though. Resizing inward is automatic. Resizing outward is another matter, and sometimes must be done purely by hand. - DonBoitnott
@DonBoitnott You are right.I tried the same and resized by calculating size.Problemis when i want to put the tabpage back to original form.How to preserve its original size.? - lokendra jayaswal
Original size? As in prior to having resized at all? Store it in a local, private Size member. - DonBoitnott

3 Answers

0
votes

I think the better way is to change size of others controls accourding to your form resize.

    private void Form1_Resize(object sender, EventArgs e) //form resize event
    {
      grdView1.SetBounds(Left,Top, this.Width-10,this.Height-10);

      grdView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
      grdView1.Columns[0].FillWeight = 45;

      //same for other columns according to your requirments.

    }

and also set the size of tabpage accourding to Form size.

0
votes

I used the code below and it worked. I kept the datagridview inside splitcontainer.Made dock property of Splitcontainer to fill and kept second panel as fixed panel.Calculated height based on Rowcount and Panels height and updated the Form Height.This way it worked. enter image description here

    int height = this.Height;
    CalculateFormHeight(ref height);
    this.Size = new Size(this.Width, height);

    private void CalculateFormHeight(ref int height)
    {
        if (dataGridViewToDisplay != null && dataGridViewToDisplay.Rows != null)
        {
            if (dataGridViewToDisplay.Rows.Count >= 15)
            {
                height = dataGridViewToDisplay.Rows[0].Height * 18 + splitContainer1.Panel2.Height;
            }
            else if (dataGridViewToDisplay.Rows.Count < 15)
            {
                height = dataGridViewToDisplay.Rows[0].Height * (dataGridViewToDisplay.Rows.Count + 3) + splitContainer1.Panel2.Height;
            }
        }
    }
0
votes

to do this without much problem you have to remove the form from the tab and then place it again

 private void frmMaster_Resize(object sender, EventArgs e)
{
  foreach (TabPage tab in tabWindows.TabPages)
            {
                foreach (Control con in tab.Controls)
                {

                    if (con is Form)
                    {

                        this.Controls.Add(con);

                      //  Thread.Sleep(20);
                        con.Size = (new Size(tab.Width, tab.Height));
                        
                        tab.Controls.Add(con);

                        //con.Width = tab.Width;
                        //con.Height=  tab.Height;
                    }

                }
            }

}