开发者

Add DataGrid DateTime columns dynamically

开发者 https://www.devze.com 2023-03-24 13:47 出处:网络
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 choo

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.

Add DataGrid DateTime columns dynamically


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}}"

0

精彩评论

暂无评论...
验证码 换一张
取 消