gpt4 book ai didi

c# - 如何使 DataGridView 的宽度和高度适合其内容?

转载 作者:行者123 更新时间:2023-11-30 15:21:22 28 4
gpt4 key购买 nike

我有一个动态创建 DataGridView 的方法,但是当它显示时,宽度大于内容宽度(列的总宽度)。高度也没有足够的长度来满足行的长度。

我试过使用这个方法,但没有成功:

    DataGridView CreateInputBox(int proc, int mac)
{
DataGridView databox = new DataGridView();
for (int i = 0; i < mac; i++)
{
databox.Columns.Add("col" + (i + 1), "M" + (i + 1));
databox.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
databox.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
databox.RowTemplate.DefaultHeaderCellType = typeof(CustomHeaderCell);
databox.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
databox.RowHeadersDefaultCellStyle.Padding = new Padding(2);
for (int i = 0; i < proc; i++)
{
databox.Rows.Add();
databox.Rows[i].HeaderCell.Value = "P" + (i + 1);
}
databox.DefaultCellStyle.SelectionBackColor = Color.LightGray;
databox.AllowUserToAddRows = false;
databox.AllowUserToDeleteRows = false;
databox.AllowUserToOrderColumns = false;
databox.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
databox.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

//This block doesn't work
var totalHeight = databox.Rows.GetRowsHeight(DataGridViewElementStates.None);
var totalWidth = databox.Columns.GetColumnsWidth(DataGridViewElementStates.None);
databox.Width = totalWidth;
databox.Height = totalHeight;
//
return databox;
}
public class CustomHeaderCell : DataGridViewRowHeaderCell
{
protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
{
var size1 = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
var value = string.Format("{0}", this.DataGridView.Rows[rowIndex].HeaderCell.FormattedValue);
var size2 = TextRenderer.MeasureText(value, cellStyle.Font);
var padding = cellStyle.Padding;
return new Size(size2.Width + padding.Left + padding.Right, size1.Height);
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.Background);
base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
TextRenderer.DrawText(graphics, string.Format("{0}", formattedValue), cellStyle.Font, cellBounds, cellStyle.ForeColor);
}
}

结果:

enter image description here

如您所见,DataGridView 控件的宽度非常长。我怎样才能使它适合两个维度?

最佳答案

主要问题是您新创建的 DataGridView 在添加到父容器之前尚未完成其内部布局,并且仍会报告所有列的宽度为 100。

修复它的一种方法是在 DGV 显示后调用调整大小函数:

void sizeDGV(DataGridView dgv)
{
DataGridViewElementStates states = DataGridViewElementStates.None;
dgv.ScrollBars = ScrollBars.None;
var totalHeight = dgv.Rows.GetRowsHeight(states) + dgv.ColumnHeadersHeight;
totalHeight += dgv.Rows.Count * 4 // a correction I need
var totalWidth = dgv.Columns.GetColumnsWidth(states) + dgv.RowHeadersWidth;
dgv.ClientSize = new Size(totalWidth , totalHeight );
}

enter image description here

请注意,我在此过程中修复了一些问题:

  • 宽度和高度均不包括标题
  • 更改外部尺寸会忽略边框。我改为更改 ClientSize。
  • 在尺寸生效之前,我们需要关闭滚动条。

关于c# - 如何使 DataGridView 的宽度和高度适合其内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37661303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com