I have these DataGridViews, used pretty heavily in my app to show lists of child or summary data. The column widths are set to handle most cases, and to fit in the default size of the UserControls that directly contain and manage the DGVs (the UserControl also contains a title, sum of records, and a Refresh button).
I would like to give the user a menu option in one screen that has a LOT of DGVs, that would basically replicate the behavior they'd get if they double-clicked on the right border of each column header they saw. The default behavior in that case is to resize that column so that all text of all cells in the column is shown. This is a freebie of using DGVs, but I开发者_高级运维'd like to plug into it to do the same thing on a wider scale.
I DO NOT want to set the AutoSize property of the columns or DGVs; if a column is resized and the user wants to further adjust it, they should be able to. There has to be a way to do this without locking the column widths to what AutoSize thinks is necessary.
OK, finally I found it:
dataGridView.AutoResizeColumn(col.Index, DataGridViewAutoSizeColumnMode.AllCells);
or if you prefer to resize all columns in one shot:
dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnMode.AllCells);
精彩评论