适合的dataGridView大小行的和列的总大小 [英] fit dataGridView size to row's and columns's total size

查看:260
本文介绍了适合的dataGridView大小行的和列的总大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个DataGridView的大小,使其适合列和行总大小。关于总高度,我设法它适合栏的高度这样的:

I want to make a dataGridView's size to fit the columns and rows total size. 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?

感谢。

推荐答案

这应该工作:

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);

本+ 2的宽度高度的值是一个忽悠的因素要占在DataGridView的宽度内饰元素。我记得看到code的地方,可以让你得到这个值,而硬codeD的数字,但我找不到它了。

The + 2 to the width and height values are a fudge factor to account for the width of the DataGridView's interior elements. 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.

这篇关于适合的dataGridView大小行的和列的总大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆