如何使datagridview可自动调整大小 [英] How to make datagridview autoresizeable
问题描述
你好程序员。我需要帮助。我正在使用Windows窗体中的Project。在这个项目中,我从Excel文件中获取数据,而不是在datagtidview中显示这些数据。我想自动调整datagridview。例如,当它有2行时,dgv高度必须等于2行。对于宽度相似。我有代码但不幸的是,当DVG中的数据太多时,它有最大高度,但它不是Scroling。对于宽度,它工作完美但高度是一个问题。滚动功能不起作用。这是一个代码:
Hello programmers. I need a Help. I am working on my Project in Windows Forms. In this Project I am getting data from Excel file and than I am displaying this data in datagtidview. I want to autoresize datagridview. For Example when it has 2 rows, dgv height must be equal to 2 rows. For width similar . I have code for that but unfortunately, when data in DVG is too much, it has max height, but it is not Scroling.For width it works perfect but Height is a Problem. Scroling function donn't works. here is a Code:
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);
推荐答案
啊,新鲜出版机:调整表单以适应DataGridView [ ^ ]
Ah, fresh off the presses: Resize Form To Fit DataGridView[^]
什么是ContainerControl(Form?,Panel?)现在是DataGridView吗?
请用DataGridView描述你现在在做什么(或者不做):
属性:
ScrollBars,ColumnHeadersHeightSizeMode,RowHeadersWidthSizeMode,AllowUserToResizeRows,AllowUserToReSizeColumns,AutoSizeRowsMode,AutoSizeColumnsMode,Anchor,Dock,MinimumSize,MaximumSize
活动:
您是否尝试过定义SizeChanged EventHandler,RowHeadersWidthChanged处理程序,ColumnHeadersHeightChanged处理程序或UserAddedRow处理程序?
What ContainerControl (Form ?, Panel ?) is the DataGridView sited in now ?
Please describe what you are doing now (or not doing) with the DataGridView:
Properties:
ScrollBars, ColumnHeadersHeightSizeMode, RowHeadersWidthSizeMode, AllowUserToResizeRows, AllowUserToReSizeColumns, AutoSizeRowsMode, AutoSizeColumnsMode, Anchor, Dock, MinimumSize, MaximumSize
Events:
Have you tried defining a SizeChanged EventHandler, or a RowHeadersWidthChanged handler, or a ColumnHeadersHeightChanged handler, or a UserAddedRow handler ?
这篇关于如何使datagridview可自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!