What better way to align columns of DataGridView C #

0
What is the best way to align the columns of a DataGridView, because if you placed to align by the size of the column name you cut cells that are larger than the size of the column name, and when placed to align by the cell and the cell size is smaller than the header, the header is trimmed, I'm starting to mess with Windows Forms part now and I'm in doubt, what is the best way to display the Grid records?

    
asked by anonymous 06.06.2017 / 19:19

2 answers

1

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();

Reference: How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?

    
06.06.2017 / 20:30
0

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.

link

    
06.06.2017 / 19:31