2011-07-30 73 views
2

我試圖在DataGrid中動態添加DateTime(7/30/2011)列。我將上傳我手動製作的網格屏幕截圖。我想製作一個範圍組合框。因此,如果用戶選擇2周,那麼網格會逐日添加列。動態添加DataGrid日期時間列

Screenshot

+0

提示:請確保您使用瀏覽器拼寫檢查。將其設置爲英語。 –

回答

2

你可以做這樣的事情

private void AddColumns(GridView gv, string[] dateColumns) 
{ 
    for (int i = 0; i < dateColumns.Length; i++) 
    { 
     gv.Columns.Add(new GridViewColumn 
     { 
      Header = dateColumns[i], 
      DisplayMemberBinding = new Binding(String.Format("[{0}]", i)) 
     }); 
    } 
} 

這可以叫上了ComboBox OnSelectionChanged()

你也可以使用一個DataTemplate正確顯示的列:

<DataTemplate DataType="{x:Type DateTime}"> 
     <TextBlock Text="{Binding StringFormat={0:d}}" /> 
</DataTemplate> 

不,只是調整y我們對您的需求的StringFormat:

基本是Binding="{Binding date, StringFormat={}{0:dd/MM/yyyy}}"

+0

以及我如何創建日期列? –

+0

@酸查看編輯 –

+0

感謝重播,但它對我來說很不清楚:(我必須這樣做?:綁定=「{綁定日期,StringFormat = {} {0:dd/MM/yyyy}}」 –