I have a DataGrid on a Win Form that display perfectly when I initially set the datasource. The AutoSizeColumnsMode is "DisplayCells". When I change the datasource during runtime the rows do not autosize u开发者_高级运维nless I re-order a column. Does anyone know how to fix this? Also note that the DataGrid is on a different Tab than the button that calls the update.
The only code that I ever use to populate the grid is:
dgUnPrinted.DataSource = TableName;
dgUnPrinted.Refresh();
try http://www.codeproject.com/KB/miscctrl/AutoResizeDataGrid.aspx or http://www.hanselman.com/blog/HowDoIAutomaticallySizeAutosizeColumnsInAWinFormsDataGrid.aspx
or use DataGridView then you can play with gdv.Columns[0].AutoSizeMode property.
Did you try the AutoResizeColumns
method?
http://msdn.microsoft.com/en-us/library/ms158595%28v=VS.90%29.aspx
After some thinking I decided to sort a column through code. This filled out all the cells. I am not sure why this is needed or why it works when everything else does not work but at least i have a fix.
dgUnPrinted.Sort(dgUnPrinted.Columns[0], ListSortDirection.Ascending);
精彩评论