Use this extension in your code, it sets the datagridview
, and leaves the columns "free" if the user wants to resize, and already formats the columns that are decimal.
public static class Extensions
{
public static void AjeitaDataGridView(this DataGridView dataGridView)
{
//para deixar o tamanho "certo e editavel" o tamanho da coluna
// all cells bloqueia o usuario a nao editar
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
for (int i = 0; i < dataGridView.Columns.Count; i++)
{
int colw = dataGridView.Columns[i].Width;
if (dataGridView.Columns[i].ValueType == typeof(Decimal))
{
dataGridView.Columns[i].DefaultCellStyle.Format = "N2";
}
//
dataGridView.Columns[i].Width = colw;
}
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
}
}
and in your code just call ..
this.datagridview1.AjeitaDataGridView();
I use:
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
The column widths are adjusted so that the contents of all the cells in the columns, including the header cells, fit into it.