2012-01-02 67 views
4

試圖設置rowHeight的這樣的(代碼):的DataGridView設置行高度不起作用

dgvTruckAvail.RowTemplate.Height = 48; 

犯規的工作。我也嘗試設置我添加的每個柱的高度 - 不起作用。

這裏是網格屬性:

this.dgvTruckAvail.AllowUserToAddRows = false; 
this.dgvTruckAvail.AllowUserToDeleteRows = false; 
this.dgvTruckAvail.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right))); 
this.dgvTruckAvail.BackgroundColor = System.Drawing.Color.White; 
this.dgvTruckAvail.BorderStyle = System.Windows.Forms.BorderStyle.None; 
this.dgvTruckAvail.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; 
this.dgvTruckAvail.Columns.AddRange(
    new System.Windows.Forms.DataGridViewColumn[] 
    { 
     this.colMon, 
     this.colTue, 
     this.colWED, 
     this.colThu, 
     this.colFri, 
     this.colSat, 
     this.colSun}); 
this.dgvTruckAvail.Cursor = System.Windows.Forms.Cursors.Default; 
dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; 
dataGridViewCellStyle8.BackColor = System.Drawing.SystemColors.Window; 
dataGridViewCellStyle8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
dataGridViewCellStyle8.ForeColor = System.Drawing.SystemColors.ControlText; 
dataGridViewCellStyle8.SelectionBackColor = System.Drawing.SystemColors.Window; 
dataGridViewCellStyle8.SelectionForeColor = System.Drawing.SystemColors.ControlText; 
dataGridViewCellStyle8.WrapMode = System.Windows.Forms.DataGridViewTriState.False; 
this.dgvTruckAvail.DefaultCellStyle = dataGridViewCellStyle8; 
this.dgvTruckAvail.EnableHeadersVisualStyles = false; 
this.dgvTruckAvail.Location = new System.Drawing.Point(0, 22); 
this.dgvTruckAvail.Margin = new System.Windows.Forms.Padding(4); 
this.dgvTruckAvail.Name = "dgvTruckAvail"; 
this.dgvTruckAvail.ReadOnly = true; 
this.dgvTruckAvail.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; 
this.dgvTruckAvail.RowTemplate.Height = 48; 
this.dgvTruckAvail.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.True; 
this.dgvTruckAvail.ShowCellToolTips = false; 
this.dgvTruckAvail.Size = new System.Drawing.Size(1098, 394); 
this.dgvTruckAvail.TabIndex = 0; 

我沒有暴食格來填充。行手動添加並手動填充單元格。 你對我還能嘗試設置它有什麼建議嗎?也許重寫網格本身?

回答

8

設置Height屬性確實有效。

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    row.Height = 80; 
} 
+1

您更正。看到我的其他問題(我發現它爲什麼不適合我)http://stackoverflow.com/questions/8705640/datagridview-setting-row-height-in-code-and-disable-manual-resize – katit 2012-01-02 21:47:49

+1

你必須將'DataGridView.Visible'屬性設置爲'true'後設置行。爲'DataGridView.VisibleChanged'事件編寫一個事件處理程序,因此當'Visible == true'時,然後設置每行的'高度'。 – gmlobdell 2013-06-27 20:53:58

+1

這樣更高效:dg.RowTemplate.Height = 30; – 2017-01-10 14:16:11

14

兩個想法:

1)設置之前RowTemplate.Height 你綁定DGV

2)設置AutoSizeRowsMo​​de =無

任何一個或兩個的這些可能的幫助。

1

設置爲AutoSizeRowsMo​​deAllCells