int height = this.dataGridView1.ColumnHeadersHeight; foreach (DataGridViewRow row in this.dataGridView1.Rows) { height += row.Height; } int width = this.dataGridView1.RowHeadersWidth; foreach (DataGridViewColumn col in this.dataGridView1.Columns) { if (col.Visible) width += col.Width; }
this.dataGridView1.Size = new Size(width+2,height+2);
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
height += row.Height;
}
int width = this.dataGridView1.RowHeadersWidth;
foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
if (col.Visible)
width += col.Width;
}
this.dataGridView1.Size = new Size(width+2,height+2);