【问题标题】:fit dataGridView size to row's and columns's total size使 dataGridView 大小适合行和列的总大小
【发布时间】:2017-11-30 01:59:15
【问题描述】:

我想使 dataGridView 的大小适合列和行的总大小。关于总高度,我设法将它与列的高度相适应:

const int datagridLines = 30;
s.Height = dataGridView2.Columns[0].HeaderCell.Size.Height;
for (byte i = 0; i < datagridLines; i++)
{
  dataGridView2.Rows.Add();
  s.Height += dataGridView2.Rows[i].Height;
}
dataGridView2.ClientSize = s;

我尝试了一些东西来适应宽度,但没有运气。有什么建议吗?

谢谢。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    这应该可行:

    int height = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows) {
        height += row.Height;
    }
    height += dataGridView1.ColumnHeadersHeight;
    
    int width = 0;
    foreach (DataGridViewColumn col in dataGridView1.Columns) {
        width += col.Width;
    }
    width += dataGridView1.RowHeadersWidth;
    
    dataGridView1.ClientSize = new Size(width + 2, height + 2);
    

    width 和 height 值的 + 2 是用于说明 DataGridView 内部元素宽度的因素。我记得在某处看到代码可以让您在没有硬编码数字的情况下获得此值,但我现在找不到。

    【讨论】:

    • 会不会是 Padding 属性对 +2 偏移值有贡献?请参阅stackoverflow.com/questions/7193268/… 接受的答案。
    • @TheBlastOne - Padding 属性设置单元格边缘与其内容之间的空间。我相信 DataGridViewColumn.DividerWidth 属性与它有关。
    • 我不会自己计算,而是看看 ticcky 的回答:stackoverflow.com/a/12455252/467172。
    • 工作就像一个魅力。
    【解决方案2】:

    Rows 和 Columns 集合的 GetRowsHeight 和 GetColumnsWidth 方法应该可以轻松地为您提供所需的内容:

    var totalHeight = dataGridView2.Rows.GetRowsHeight(DataGridViewElementStates.None);
    var totalWidth = dataGridView2.Columns.GetColumnsWidth(DataGridViewElementStates.None);
    

    【讨论】:

    • 这不包括标题高度/宽度。
    • 你可以做 totalwidth+=dataGridView1.RowHeadersWidth;
    【解决方案3】:

    更完整的版本:

    dataGridView.Height = dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.None) + dataGridView.ColumnHeadersHeight + 2;
    
    dataGridView.Width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dataGridView.RowHeadersWidth + 2;
    

    【讨论】:

    • 值得注意的是,它结合了 ticcky 和 ​​jay 的优点,但您应该详细说明“2”。
    • 不适用于这种情况,尽管我有一个效率低下的方法stackoverflow.com/questions/37635932/…
    【解决方案4】:

    您需要使用 DataGridViewRow 集合和 DataGridViewColumn 集合的属性来执行此操作。

    尝试类似于本文中使用的方法:

    how to increase rows size in DataGridView

    【讨论】:

    • 感谢您的回答,Mamta Dalal。
    【解决方案5】:

    在视觉基础上有点晚了,但这对我有用,以防万一 其他人找到了这个线程。我在 tableLayoutPanel 中使用停靠的 datagridview,它会在调整表单大小时自动调整大小。它不会缩小表格。我只是从小表格开始。可以使用相同的技术添加行。

    Private Sub setFormWidth()
            Dim iTotalWidth As Integer = 0 '884
            Dim iDisplayedWidth As Integer = 0
            Dim r As Rectangle
            For Each c As DataGridViewColumn In dgv_EmpList.Columns
                If c.Visible Then
                    iTotalWidth += c.Width
                    r = dgv_EmpList.GetColumnDisplayRectangle(c.Index, True)
                    iDisplayedWidth += r.Width
                End If
            Next
            Me.Width += (iTotalWidth - iDisplayedWidth)
        End Sub
    

    【讨论】:

    • 块引用适用于引用其他来源的情况。此外,除非您的表单是无边框的,否则您应该改用 Me.ClientSize,因为这会排除边框(在不同的 Window 版本上具有不同的值)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2016-07-02
    • 2013-11-22
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多