简体   繁体   中英

How to set rows' height to automatically resize so contents fit

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:

订单描述在一行。图像单元格未放大。

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.

datagridview1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
datagridview1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders

datagridview的属性AutoSizeColumnMode设置为AllCells并检查它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM