Question

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.

Was it helpful?

Solution 2

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;
            }
        }
    }

OTHER TIPS

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.

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;
                    }

                }
            }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top