简体   繁体   中英

Set DataGridView column[0] width to fit largest text from the column?

I have a Datagridview which I populate, row-by-row:

while (!proc.StandardOutput.EndOfStream)
{
    string line = proc.StandardOutput.ReadLine();
    string[] row = getRow();
    dataGridView1.Rows.Add(row);
}

How can I size column 0 to fit the longest line of text from that column (across all rows)?

EDIT: I have also noticed if I manually drag the width of column 0 to the edge of my window, the horizontal scrollbar disappears, preventing me from scrolling to see the remaining columns?

You can set AutoSizeMode property of the column to one of these values: AllCells , AllCellsExceptHeader , DisplayedCells or DisplayedCellsExceptHeader . Then the column width adjusts to fit the contents of all or displayed cells in the column, including or except the header cell. For more information see DataGridViewAutoSizeColumnMode enumeration.

dataGridView1.Columns[0].AutoSizeMode= DataGridViewAutoSizeColumnMode.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