المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : WinForms - DataGridView AutoResizeColumns not working.



C# Programming
12-05-2009, 03:50 PM
I have a simple class (just contains automatic properties, nothing special), and a Form w/ a datagridview on it. It is binding directly to the class, and is working fine in every respect, however I want to autosize the columns to the data within them after bind (but retain it's current column sizing mode of 'None' afterwords so the user can resize them)
I call AutoResizeColumns and pass DataGridViewAutoSizeColumnsMode.DisplayedCells
(I've also tried 'AllCells'). The datagrid doesn't resize though! Am I missing something?


private void PopulateUI() {
this.dataGridViewHistory.AutoGenerateColumns = false;
this.dataGridViewHistory.DataSource = this.Project.Modifications;
//the next line doesn't appear to do anything
this.dataGridViewHistory.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.DisplayedCells );
}


//
// dataGridViewHistory initialization code (from designer.cs)
//
this.dataGridViewHistory.AllowUserToAddRows = false;
this.dataGridViewHistory.AllowUserToDeleteRows = false;
this.dataGridViewHistory.AllowUserToOrderColumns = true;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.PapayaWhip;
this.dataGridViewHistory.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridViewHistory.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewHistory.Columns.AddRange( new System.Windows.Forms.DataGridViewColumn[] {
this.ColumnUserID,
this.ColumnUserName,
this.ColumnDate,
this.ColumnFileName} );
this.dataGridViewHistory.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridViewHistory.******** = new System.Drawing.Point( 6, 16 );
this.dataGridViewHistory.Name = "dataGridViewHistory";
this.dataGridViewHistory.ReadOnly = true;
this.dataGridViewHistory.RowHeadersVisible = false;
this.dataGridViewHistory.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridViewHistory.Size = new System.Drawing.Size( 550, 236 );
this.dataGridViewHistory.TabIndex = 0;