[英]fit dataGridView size to row's and columns's total size
I want to make a dataGridView's size to fit the columns and rows total size. 我想使dataGridView的大小适合列和行的总大小。 About total height, I managed to fit it to columns's height like that:
关于总高度,我设法使它适合列的高度:
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;
I tried some things to also fit the width but no luck. 我试过一些东西也适合宽度但没有运气。 Any suggestions?
有什么建议?
Thanks. 谢谢。
This should work: 这应该工作:
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);
The + 2 to the width
and height
values are a fudge factor to account for the width of the DataGridView's interior elements. width
和height
值的+ 2是一个软糖因子,用于说明DataGridView内部元素的宽度。 I recall seeing code somewhere that will allow you to get this value without the hard coded numbers but I can't find it now. 我记得在某个地方看到代码可以让你在没有硬编码数字的情况下得到这个值,但我现在找不到它。
GetRowsHeight and GetColumnsWidth methods of Rows and Columns collections should give you what you need in an effortless way: 行和列集合的GetRowsHeight和GetColumnsWidth方法应该可以毫不费力地为您提供所需的内容:
var totalHeight = dataGridView2.Rows.GetRowsHeight(DataGridViewElementStates.None);
var totalWidth = dataGridView2.Columns.GetColumnsWidth(DataGridViewElementStates.None);
A bit more complete version: 更完整的版本:
dataGridView.Height = dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.None) + dataGridView.ColumnHeadersHeight + 2;
dataGridView.Width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dataGridView.RowHeadersWidth + 2;
It's a tad late and in visual basic, but this worked for me in case someone else finds this thread. 这有点晚了,在视觉基础上,但这对我有用,以防其他人找到这个帖子。 I'm using a docked datagridview in a tableLayoutPanel, which automatically resizes when the form is resized.
我在tableLayoutPanel中使用停靠的datagridview,它在调整窗体大小时自动调整大小。 It doesn't shrink the form.
它不缩小形式。 I just start the form small.
我只是开始小形式。 Rows could be added using the same technique.
可以使用相同的技术添加行。
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
You will need to use the properties of the DataGridViewRow collection and DataGridViewColumn collection to do this. 您需要使用DataGridViewRow集合和DataGridViewColumn集合的属性来执行此操作。
Try the approach similar to the one used in this: 尝试类似于此处使用的方法:
how to increase rows size in DataGridView 如何在DataGridView中增加行大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.