9

I'm working on DataGridView and was wondering if there's a property that automatically makes cells bigger if the content requires it.

So far I have, at the end of the DataGridViewBindingComplete handler:

dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);

But unfortunately, that didn't do the trick.

Here's what I tried so far:

public partial class Form1 : Form
{
    private void dgv1BindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
    }

    public Form1()
    {
        InitializeComponent();

        // [...] set up datasource: orders

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        dataGridView1.DataSource = orders;

        DataGridViewTextBoxColumn idCol = new DataGridViewTextBoxColumn();
        idCol.DataPropertyName = "id";
        idCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
        idCol.HeaderText = "#";
        idCol.DisplayIndex = 0;

        DataGridViewTextBoxColumn placedCol = new DataGridViewTextBoxColumn();
        placedCol.DataPropertyName = "placed";
        placedCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
        placedCol.HeaderText = "Time Placed";
        placedCol.DisplayIndex = 1;

        // [...] more of these columns

        dataGridView1.Columns.Add(idCol);
        dataGridView1.Columns.Add(placedCol);
        // [...] adding the rest of the columns

        dataGridView1.DataBindingComplete += dgv1BindingComplete;
    }
}

With the following result:

Orders description is on one line. Image cell is not enlarged.

lowerkey
  • 8,105
  • 17
  • 68
  • 102

3 Answers3

13

The answer was hidden in another Stackoverflow question: How to set DataGridView textbox column to multi-line?

Setting the DefaultCellStyle.WrapMode to TriState.True did the trick.

Community
  • 1
  • 1
lowerkey
  • 8,105
  • 17
  • 68
  • 102
6
datagridview1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
datagridview1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
Shafick Cruz
  • 61
  • 1
  • 3
  • This is clear and concise answer. In my case I had increased the font size and the row height didn't change. Somehow I missed the property in the Layout section. – David Bradley Mar 18 '23 at 15:46
1

Set property AutoSizeColumnMode of datagridview to AllCells and check it.

Priyank
  • 1,219
  • 11
  • 30