DataGridView ,调整 DataTable 的宽度和高度
我正在将 DataTable 绑定到 GridView。它不会调整数据表的高度和宽度。如果行很少,我如何拉伸显示所有列的网格的宽度和高度缩小。
I am binding a DataTable to GridView. It does not adjust to height and width of the DataTable. How can I the strech the width of the grid that i shows all the columns and height shrink if the rows are few.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只需转到数据网格的属性 =>
然后在 Layout=> 部分
AutoSizeColumnsMode 将其设置为 Fill...
just go to the properties of your datagrid =>
and then at the section Layout=>
AutoSizeColumnsMode set it to Fill...
要自动调整列大小以适合数据(宽度方向),然后自动调整表单大小以适合 gridview(宽度方向),请使用以下代码:
其中 dataGridView1 是本示例中 Datagridview 的名称,
this
指的是表单的实例。 100 是一个常量,表示您希望表单比 datagridview 宽多少像素。 (注意:您可能需要进行一些宽度检查,以确保表单和数据 gridview 不比用户的屏幕宽)自动调整 datagridview 的大小以适合行(高度),然后自动调整表单的大小以适合 gridview (高度) -wise),使用以下代码:
其中 dataGridView1 是本示例中 Datagridview 的名称,
this
指的是表单的实例。 100 是一个常数,表示您希望表单比 datagridview 高多少像素。 (注意:您可能需要进行一些高度检查,以确保表单和数据网格视图不高于用户的屏幕)To autosize the columns to fit the data (width-wise) and then autosize the form to fit the gridview (width-wise), use the following code:
where dataGridView1 is the name of the Datagridview in this example, and
this
is refering to the instance of the form. The 100 is a constant for how many pixels wider you want the form than the datagridview. (note: you may want some width checking to make sure you form and data gridview aren't wider than the user's screen)To autosize the datagridview to fit the rows (height-wise) and then autosize the form to fit the gridview (height-wise), use the following code:
where dataGridView1 is the name of the Datagridview in this example, and
this
is refering to the instance of the form. The 100 is a constant for how many pixels higher you want the form than the datagridview. (note: you may want some height checking to make sure you form and data gridview aren't taller than the user's screen)首先,您的表单必须足够大,以便 DataGridView 能够展开并显示所有列。在屏幕截图中,它看起来不够宽。您可以手动设置 datagridview 的大小,但在设计时很少知道每列的确切宽度,因为您不知道运行时每列的宽度。例如,您的 Invoicedescription 列可能包含长度不同的字符串。
在像您这样的情况下,我通常所做的就是在表单上放置一个拆分器控件。顶部的文本框和导入发票按钮将位于拆分器控件的顶部面板中,DataGridView 将位于底部面板中。然后,设置 DataGridView 的 Dock 属性以填充拆分器控件的底部面板。这样,当用户调整表单大小时,DataGridView 将随之增大/缩小。
您可以通过设置 DataGridView.AutoResizeRows 属性进一步控制列的显示方式。
只是将其作为一个选项,我不知道有什么方法可以让网格自动调整大小以适应其显示的数据。您也许可以计算行的高度/宽度,然后在代码中手动调整网格的大小,但我会首先确保我确实需要该要求。
First, your form would have to be big enough for the DataGridView to expand and show all of the columns. In the screenshot, it doesn't look wide enough. You can set the size of the datagridview manually, but it is rare that you know the exact width of each column at design time because you don't know how much will be in each column at run time. For example, your Invoicedescription column could have strings that vary in length.
What I typically do in cases like yours is to put a splitter control on the form. The text box at the top and the Import Invoices button would go in the top panel of the splitter control, and the DataGridView would go in the bottom panel. Then, set the Dock property of the DataGridView to fill the bottom panel of the splitter control. This way, when the user resizes the form the DataGridView will grow/shrink with it.
You can further control the way the columns appear by setting the DataGridView.AutoResizeRows property.
Just giving this as an option, I'm not aware of any way to have the grid automatically resize to the data it is displaying. You may be able to calculate the height/width of the rows and then manually resize the grid in code, but I would make sure I really needed that requirement first.
假设所有行都具有相同的高度,即使第一个标题行具有不同的高度(例如长标题被换行),这也是可行的
This works assuming that all the rows have the same height, even when the first header row has different height (for example long headers are wrapped)
将 datagridviewautosizecolumn 设置为“全部”
然后,在包含 datagridview 的表单中,假设您根据需要在加载事件处理程序或其他一些事件处理程序中填充数据网格视图,无论数据加载到何处(必须在数据加载之后),键入: (VB)
Me.Width = DataGridView1.Width + 50 ' 比您希望表单的 datagridview 宽的像素数。
datagridview1 是 datagridview 控件的名称。
Me 指的是包含 datagridview 控件的表单。
您可能需要最大化窗口或将其锚定到顶部和左侧以确保适合,具体取决于填充的数据集的大小。
Set the datagridviewautosizecolumn to "All"
Then, in the form that contains the datagridview, assuming you populate the data grid view in the load event handler or some other event handler as desired wherever the data is loaded (must be after data is loaded) type: (VB)
Me.Width = DataGridView1.Width + 50 ' Number of pixels wider than the datagridview you want the form to be.
datagridview1 is the name of your datagridview control.
Me refers to the form that contains the datagridview control.
You may need to either maximize the window or anchor it to the top and left to ensure fitment depending on how large of a dataset is being populated.
我知道这可能已经得到解答,但以防万一我在这里发布什么帮助我通过 AllCells 自动调整整个数据网格视图的大小:
在加载数据源后,粘贴以下内容
I know this is probably already answered but just in case I post here what helped me to autosize the whole datagrid view by AllCells:
After your Datasource load, paste the following
我在 IronPyhton 中使用它来适应表
1:
LayoutPanel.AutoSize = True
2:dataGridView.AutoSize = True
3:
self.AutoSize = True
i used this in IronPyhton to fit the table
1:
LayoutPanel.AutoSize = True
2:
dataGridView.AutoSize = True
3:
self.AutoSize = True