我们常听到一些用户的需求:随着Winform窗体尺寸发生变化,Spread行和列的比例能够Resize。由于FpSpread没有内置实现的接口,为了达到同样的效果,需要自定义实现。这篇博文说明了FpSpread的列和行的比例,是随着窗体大小尺寸变化而调整的。
实现非常简单,需要处理Winform窗体的Resize事件。在这种情况下,每个列和行被分配了不同width和height分别根据已调整大小的表格的大小。
下面的代码段显示窗体的Resize事件,根据计算不断变化的的尺寸,来调整Spread中的行和列的高和宽。
1: void Form1_Resize(object sender, EventArgs e)
2: {
3: fpSpread1.Location = new Point(0, 0);
4: fpSpread1.Height = (int)(0.8 * this.Height);
5: fpSpread1.Width = (int)(0.8 * this.Width);
6: int i;
7: float dataareawidth;
8: float rhwidth = 0;
9: float bwidth;
10: float vsbwidth;
11: float dataareaheight;
12: float chheight = 0;
13: float hsbheight;
14: // Horizontal ScrollBar Height
15: bwidth = (2 * SystemInformation.Border3DSize.Width);
16: for (i = 0; (i <= (fpSpread1.Sheets[0].RowHeader.ColumnCount - 1)); i++)
17: {
18: rhwidth += fpSpread1.Sheets[0].RowHeader.Columns[ i ].Width;
19: }
20: vsbwidth = SystemInformation.VerticalScrollBarWidth;
21: dataareawidth = (fpSpread1.Width - (bwidth - (rhwidth - vsbwidth)));
22: for (i = 0; (i <= 3); i++)
23: {
24: // Columns
25: fpSpread1.Sheets[0].Columns[ i ].Width = (dataareawidth / 4);
26: }
27: for (i = 0; (i <= (fpSpread1.Sheets[0].ColumnHeader.RowCount - 1)); i++)
28: {
29: chheight += fpSpread1.Sheets[0].ColumnHeader.Rows[ i ].Height;
30: }
31: hsbheight = SystemInformation.HorizontalScrollBarHeight;
32: dataareaheight = ((float)fpSpread1.Height - (bwidth - (chheight - hsbheight)));
33: for (i = 0; (i <= 9); i++)
34: {
35: // Rows
36: fpSpread1.Sheets[0].Rows[ i ].Height = (dataareaheight / 10);
37: }
38: }