2013-02-08 45 views
2

如何在UltraGrid中準確獲取擴展的UltraGridColumn寬度?獲取超網格的實際寬度

我有以下的Windows窗體:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     IEnumerable<string> test = new List<string>{DateTime.Now.ToString()}; 
     this.ultraGrid2.DataSource = test; 
     this.ultraGrid2.DisplayLayout.AutoFitStyle = AutoFitStyle.ExtendLastColumn; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("The column width is " + this.ultraGrid2.DisplayLayout.Bands[0].Columns[0].Width); 
    } 
} 

what the form looks like

不管我如何操作或調整窗體。 width仍然給出相同的默認值。只有當我調整列的大小時,它纔會改變。在這種情況下,我認爲寬度將接近表單本身的x大小。

回答

5

如果將AutoFitStyle設置爲ExtendLastColumn,則最後一列的Width屬性不會反映該列的實際寬度。如果您將AutoFitStyle設置爲None,則可以設置並讀取列寬。

但是,至少只是爲了讀書,有一個解決辦法:
在你上面的例子,只有一列,就可以得到該列的CellSizeResolved財產

UltraGridColumn column = grid.DisplayLayout.Bands[0].Columns[0]; 
Debug.WriteLine("Column width is " + column.CellSizeResolved.Width); 
+0

謝謝你的解釋和解決方法。 – 2013-02-08 21:34:54