简体   繁体   English

Win Form调整大小时如何保持Datagridview列宽的宽高比

[英]How to maintain aspect ratio of datagridview column width when win form resize

I have one win form and there is one grid. 我有一个获胜形式,只有一个网格。 Grid dock property is set to fill . Grid Dock属性设置为fill When I bind my grid with data then I also mention all column width like this way. 当我将网格与数据绑定时,我还会像这样提及所有列宽。

dgList.DataSource = ds.Tables[0];

dgList.Columns[0].Width = 100;
dgList.Columns[1].Width = 100;
dgList.Columns[2].Width = 262;
dgList.Columns[3].Width = 530;

So I like to know when I will resize my form then grid height & width will also increase proportionately. 因此,我想知道何时调整表单大小,然后网格的高度和宽度也会成比例地增加。 So when grid height & width will change then I want to increase or decrease the column with proportionately as per previous column width. 因此,当网格的高度和宽度将发生变化时,我想按先前的列宽按比例增加或减少列。 So how to achieve it. 那么如何实现它。 Thanks. 谢谢。

i got some code from stackoverflow and those are good for calculating aspect ratio 我从stackoverflow得到了一些代码,这些代码对于计算宽高比非常有用

1) var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)

And a very basic function for calculating the GCD, using the Euclidean algorithm:

static int GCD(int a, int b) {
    return b == 0 ? a : GCD(b, a % b);
}

2)
public int GCD(int a, int b)

{
    while (a != 0 && b != 0)
    {
         if (a > b)
            a %= b;
         else
            b %= a;
    }
     if (a == 0)
         return b;
     else
         return a;
}

// Using Konrad's code: 

var gcd = GCD(A, B);
return string.Format("{0}:{1}", A / gcd, B / gcd)

3)
private int FindHCF(int m, int n)
 {
     int temp, reminder;
     if (m < n)
     {
         temp = m;
         m = n;
         n = temp;
     }
     while (true)
     {
         reminder = m % n;
         if (reminder == 0)
             return n;
         else
             m = n;
         n = reminder;
     }
 }

So here is the rest of the code

int hcf = FindHcf(835, 625);
int factorW = 835 / hcf;
int factorH = 625 / hcf;

您可能正在寻找DataGridViewColumn.FillWeight属性。

For all the columns, set AutoSizeMode property to fill. 对于所有列,将AutoSizeMode属性设置为fill。 Once done, on resizing form, columns will also increase and decrease in width proportionately. 一旦完成,在调整大小形式时,列的宽度也会成比例地增加和减少。

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

相关问题 如何调整图像大小并保持宽高比? - How to resize my image and maintain the aspect ratio? 调整Window Forms应用程序大小时如何保持长宽比? - How to maintain the aspect ratio when resizing a Window Forms application? 调整PictureBox的宽度并保持宽高比 - Resize PictureBox width and keep aspect ratio 在这种情况下如何保持网格的纵横比? - How to maintain the aspect ratio of Grid in this case? 如何以编程方式保持 wpf 中对象的纵横比 - How to programmatically maintain aspect ratio of object in wpf 如何在Textblock中缩小字体大小以适合内容的宽度并保持字体长宽比 - How to shrink font size in Textblock to fit width of content AND maintain font aspect ratio 如何调整图像大小而又不影响纵横比 - How to resize the images without disturbing the aspect ratio 将JPEG图像调整为固定宽度,同时保持宽高比不变 - Resize JPEG image to fixed width, while keeping aspect ratio as it is 表单调整大小时如何调整datagridview控件的大小 - How to resize datagridview control when form resizes 我应该如何在 Unity c# 中保持纵横比并将 3D 对象设置为在所选纵横比的纵横比内缩放? - How should I Maintain aspect ratio in Unity c# and set the 3D object to scale within the aspect ratio of the selected aspect ratio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM