DataGridView设置行高不起作用

时间:2012-01-02 21:04:02

标签: c# winforms datagridview

尝试像这样设置RowHeight(在代码中):

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;

我不是想要网格填充。手动添加行并手动填充单元格。 您对我还能尝试设置的其他建议有什么建议吗?也许以某种方式覆盖Grid本身?

4 个答案:

答案 0 :(得分:14)

两个想法:

1)设置RowTemplate.Height 之前绑定DGV

2)设置AutoSizeRowsMo​​de = none

其中任何一个或两个都可能有所帮助。

答案 1 :(得分:10)

设置每个Height属性确实有效。

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

答案 2 :(得分:2)

AutoSizeRowsMode属性从无变为AllCells或任何其他值。

答案 3 :(得分:1)

AutoSizeRowsMo​​de 设置为 AllCells