简体   繁体   English

自动调整窗口大小以适合datagradview vb.net

[英]autosize window to fit datagradview vb.net

Is it possible in vb.net to autosize the form window to change it width to the datagridview width? 在vb.net中是否可以自动调整窗体窗口的大小以将其宽度更改为datagridview宽度?

I have kept looking for a while now but can only find on how to resize the datagridview itself. 我一直在寻找一段时间,但只能找到如何调整datagridview本身的大小。

This is code for prepare Datagridview 这是准备Datagridview的代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
   Dim rowvalue As String Dim cellvalue(20) As String 
   Dim streamReader As IO.StreamReader = New IO.StreamReader("test.csv") 

   While streamReader.Peek() <> -1 
     rowValue = streamReader.ReadLine() 
     cellvalue = rowvalue.Split(","c) 
     DataGridView1.Rows.Add(cellValue) 
   End While 

   streamReader.Close() 
 End Sub 

Thanks, 谢谢,

You mean the main form containing the gridview? 您是说包含gridview的主表单?

Me.Height = heightVal
Me.Width = widthVal

Depending upon the variation in the size of the gridview, you can use the two properties above to adapt the size of the main form. 根据gridview大小的变化,可以使用上面的两个属性来调整主窗体的大小。 Bear in mind that this will provoke the location of the form to be changed and thus you should also have to affect it at runtime ( Me.Left & Me.Top ). 请记住,这将引起要更改的表单的位置,因此您还必须在运行时Me.Left进行影响( Me.LeftMe.Top )。

If it is not the main form, you should replace Me , with the given object (eg, Panel1 ). 如果不是主要形式,则应将Me替换为给定的对象(例如Panel1 )。

----------------- EXAMPLE -----------------示例

    Dim rightGap As Double = 10
    Dim bottomGap As Double = 10
    If (DataGridView1.Location.X + DataGridView1.Width > Me.Width + rightGap) Then
        Me.Width = 2 * Me.Width - (DataGridView1.Location.X + DataGridView1.Width) + rightGap
    End If
    If (DataGridView1.Location.Y + DataGridView1.Height > Me.Height + bottomGap) Then
        Me.Height = 2 * Me.Height - (DataGridView1.Location.Y + DataGridView1.Height) + bottomGap
    End If

I am also including "gaps" both at the right and at the bottom of the form. 我还在表格的右侧和底部都包括“间隙”。 This code (or something on these lines) should be enough to make sure that the Height/Width values are updated at runtime without any problem (gridview always properly located inside the form). 此代码(或这些行中的内容)应足以确保在运行时更新Height / Width值而不会出现任何问题(gridview始终正确地位于表单内部)。 Lastly, you should bear in mind that the size of the datagridview does not have to match the one of columns/rows. 最后,您应该记住datagridview的大小不必与列/行之一匹配。 In order to avoid wrong resizing, you would have also to make sure that the total width of all the rows matches the width of datagridview and the total height of all the columns its height 为了避免错误地调整大小,您还必须确保所有行的总宽度与datagridview的宽度匹配,而所有列的总高度与其高度匹配


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM