Add DataGrid DateTime columns dynamically
I'm trying to add DateTime
(7/30/2011) columns dynamically in a DataGrid
. I will upload a screenshot of my grid which I made manually. I want to make a range combobox. S开发者_C百科o if the user chooses 2 weeks, then the grid adds day by day columns.
You can do something like this
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))
});
}
}
This could be called on the Combobox OnSelectionChanged()
You can also use a DataTemplate to properly display columns:
<DataTemplate DataType="{x:Type DateTime}">
<TextBlock Text="{Binding StringFormat={0:d}}" />
</DataTemplate>
No just adjust your StringFormat for your needs:
Basic is Binding="{Binding date, StringFormat={}{0:dd/MM/yyyy}}"
精彩评论