表单自动调整大小到datagridview内容

时间:2015-10-12 07:52:21

标签: c# winforms datagridview

我有一个绑定到List的DataGridView,显示在Form中。由于它是表单的主要项目,我希望表单适合DataGridView的内容。感谢此处提出的问题,我已经设置了此代码:

private void MessageForm_Shown(object sender, EventArgs e)
{
    // A few init stuff skipped
    m_isInit = true;
    init();
}

private void init(object list = null)
{
    if (!m_isInit)
    {
        return;
    }
    dataGridView1.Columns.Clear();
    dataGridView1.Rows.Clear();


    if (list == null)
    {
        list = MessageContainer.instance.messages.OrderBy(message => message.read).ThenByDescending(message => message.dateTime).ToList().toBindingList();
    }

    dataGridView1.DataSource = new BindingSource(list, null);

    int height = dataGridView1.Rows.Cast<DataGridViewRow>().Sum(row => row.Height);
    height += dataGridView1.ColumnHeadersHeight;

    int width = dataGridView1.Columns.Cast<DataGridViewColumn>().Sum(col => col.Width);
    width += dataGridView1.RowHeadersWidth;

    dataGridView1.ClientSize = new Size(width + 2, height + 2);
    Refresh();
}

当列表中有1个项目时,它很有效:

working screenshot

但是一旦名单更宽,它就会变得疯狂:

enter image description here

如何修复此代码以正确显示DataGridView?

表格有这个:

AutoSize     : True
AutoSizeMode : GrowAndShrink

没有设置最小/最大尺寸。

似乎这个问题需要更多的信息。

我的DataGridView在FlowLayoutPanel中形成,它位于From。

以下是DataGridView的属性(只是关于size / resize / autosize / style的属性 - 可以按需提供更多 - 其他值设置为 default ):

// 
// dataGridView1
// 
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
this.dataGridView1.ClipboardCopyMode = System.Windows.Forms.DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle3;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
this.dataGridView1.Location = new System.Drawing.Point(3, 3);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
this.dataGridView1.Size = new System.Drawing.Size(878, 516);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);

以下是flowLayoutPanel的属性:

// 
// flowLayoutPanel1
// 
this.flowLayoutPanel1.AutoSize = true;
this.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.flowLayoutPanel1.Controls.Add(this.clearButton);
this.flowLayoutPanel1.Controls.Add(this.btnCopyAll);
this.flowLayoutPanel1.Controls.Add(this.cboxLevel);
this.flowLayoutPanel1.Controls.Add(this.label1);
this.flowLayoutPanel1.Controls.Add(this.cboxExpertMode);
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(3, 525);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.flowLayoutPanel1.Size = new System.Drawing.Size(878, 34);
this.flowLayoutPanel1.TabIndex = 1;

这是表单属性:

// 
// MessageForm
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(884, 562);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "MessageForm";
this.Text = "Errors, always errors...";
this.Shown += new System.EventHandler(this.MessageForm_Shown);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.flowLayoutPanel1.ResumeLayout(false);
this.flowLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

我正在使用所有Dock,'AutoSize'和&amp;所以对我最好的属性尝试做我想要的,但我不能让它工作。也许我没有抓住这些属性的真正含义。

此外,MessageForm在打开时显示良好且MessageContainer.instance.messages.Count为1.如果我打开MessageFormMessageContainer.instance.messages.Count大于1,则无效

0 个答案:

没有答案