Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:00:27+00:00 2026-05-28T03:00:27+00:00

Here is my code: foreach (var columnData in lookup.DataProvider.Metadata) { DataGridColumn column = new

  • 0

Here is my code:

foreach (var columnData in lookup.DataProvider.Metadata)
    {
        DataGridColumn column = new DataGridTextColumn { Binding = new Binding(columnData.FieldName) };

        if (columnData.DataType == typeof(bool))
        {
            column = new DataGridCheckBoxColumn { Binding = new Binding(columnData.FieldName) };
        }

        if (columnData.DataType == typeof(DateTime))
        {
            column = new DataGridTemplateColumn();
            //... ????
        }

        column.Header = columnData.Caption;

        DataDataGrid.Columns.Add(column);
    }

Basically, I’m creating columns and bindings in code because columns not known at design-time.

Now I need to add templated column and not sure how to write it in C#. Here is example of XAML of column I need to add:

<sdk:DataGridTemplateColumn Header="Received" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" SortMemberPath="SomeTime">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <MyControls:MyDateTimeLabel DisplayUtcDate="{Binding SomeTime}" />
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

EDIT

In case someone interested. I used solution from here: http://www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html

I took version with XAML loader. It definitely smells since I got my namespaces etc hardcoded into strings.

So, I started to explore second choice. Here is how my dynamic column looks now:

column = new DataGridTemplateColumn
            {
                CanUserSort = true,
                SortMemberPath = columnData.FieldName,
                CellTemplate = (DataTemplate)this.Resources["DateTimeColumnDataTemplate"]
            };

I’m loading DateTemplate from resources. This was cool, but how do I do binding? Suggestion here was to get to my DateTimeLabel and set binding. But that didn’t work(see article on why). So, I wrote this code and all is well:

private void OnLoadingRow(object sender, DataGridRowEventArgs e)
    {
        foreach (DataGridColumn t in this.DataDataGrid.Columns)
        {
            if (t is DataGridTemplateColumn)
            {
                var label = t.GetCellContent(e.Row) as DitatDateTimeLabel;
                label.SetBinding(DitatDateTimeLabel.DisplayUtcDateProperty, new Binding(t.SortMemberPath));
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:00:28+00:00Added an answer on May 28, 2026 at 3:00 am

    You could put your DataTemplate inside Page/UserControl resources, retrieve it in code and apply to your column’s CellTemplate. It would look sth like this:

    column.CellTemplate = (DataTemplate)this.Resources["DateTimeFieldTemplate"];

    The binding should work as it is in your DataTemplate XAML right now because on the DataGrid row level your DataContext will be set to the item itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's my code: XmlDocument doc = new XmlDocument(); foreach (string c in colorList) {
Here's the code: ProductList products = xxx.GetCarProducts(productCount); List<CarImageList> imageList = new List<CarImageList>(); foreach(Product p
is possible this wrote simple, code is here: foreach (var friend in friends) {
Here is the code: using (var context = new AventureWorksDataContext()) { IEnumerable<Customer> _customerQuery =
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
Here is the code: $arraya = array('a','b','c'); foreach($arraya as $key=>$value) { if($value == 'b')
This is my controller code: using (var db = new MatchGamingEntities()) { var MyMatches
Here is my code: SqlConnection conn4 = new SqlConnection(Data Source=.\\sqlexpress;Initial Catalog=test_BdbCSSQL01;Persist Security Info=False;Integrated Security=SSPI;);
Here is my code var bms = from b in context.bookmark join q in
...so I have this code: foreach (var entry in list) { var marginOneEntry =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.