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