Hi,

I use DataGridView to display data in unbound mode, the data are always in n x n form (like square matrix), and I want to display it without scrollbar in vertical and horizontal direction.

I use the following code to complete this

public static void Initialize(DataGridView dgv, string[] title)
{
dgv.SuspendLayout();

dgv.Columns.Clear();

for (int i = 0; i < title.Length; i++)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

col.HeaderText = titleIdea;
col.ReadOnly = false;
col.SortMode = DataGridViewColumnSortMode.NotSortable;
dgv.Columns.Add(col);
}

dgv.Rows.Add(SysConstants.Stations.Count);

for (int i = 0; i < title.Length; i++)
{
dgv.RowsIdea.HeaderCell.Value = titleIdea;
}


dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AllowUserToOrderColumns = false;
dgv.AllowUserToResizeColumns = false;
dgv.AllowUserToResizeRows = false;
dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
dgv.MultiSelect = false;

dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.False;
dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

dgv.ColumnHeadersVisible = true;
dgv.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight;

dgv.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;

dgv.RowHeadersVisible = true;
dgv.RowHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleLeft;

dgv.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

dgv.CurrentCell = null;


// adjust size of datagridview so that there are no scrollbars in both direction

int height = dgv.ColumnHeadersHeight + 2;
for (int i = 0; i < dgv.Rows.Count; i++)
height += dgv.RowsIdea.Height;
dgv.Height = height;

int width = dgv.RowHeadersVisible ? dgv.RowHeadersWidth : 0;
for (int i = 0; i < dgv.Columns.Count; i++)
width += dgv.ColumnsIdea.Width;
dgv.Width = width + 2;

dgv.ResumeLayout();
}

Some problems

1. dgv.ColumnHeadersHeight return the same value even when the content of ColumnHeaderCell is wrapped.

2. dgv.ColumnHeadersHeight return diffenent value (say, 20 or 23), when apply this procedure to different DataGridView with same setting.

Thanks for any help.