简体   繁体   中英

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 . 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

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. Once done, on resizing form, columns will also increase and decrease in width proportionately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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