[英]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. 谢谢。
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.