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 486221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:28:52+00:00 2026-05-13T01:28:52+00:00

The following WPF code displays the contents of FirstName and ZipCode in the WPF

  • 0

The following WPF code displays the contents of FirstName and ZipCode in the WPF Toolkit DataGrid.

However, I don’t want to just display the data as it is but slightly modified, e.g. I might want to display all the zipcodes to display with a “-0000” on the end, or I may want to display “n/a” if a cell is blank.

I could only find CopyingCellClipboardContent which doesn’t seem to do what I want.

I’m thinking I might need a Converter but am not sure how to go about it in this example.

How can I manipulate the cell content of the DataGrid cells at runtime?

XAML:

<Window x:Class="TestControl3423.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window2" Height="300" Width="500">
    <StackPanel>
        <tk:DataGrid x:Name="dataGrid" 
                     Margin="0 0 0 10"
                     AutoGenerateColumns="False" 
                     CanUserAddRows="False"
                     HeadersVisibility="Column" 
                     MaxHeight="400"
                     IsReadOnly="True"
                     Background="#fff"
                     ColumnWidth="SizeToHeader">
        </tk:DataGrid>
    </StackPanel>
</Window>

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using Microsoft.Windows.Controls;

namespace TestControl3423
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            dataGrid.ItemsSource = Customer.GetCustomers();

            dataGrid.Columns.Clear();

            DataGridTextColumn dgtc1 = new DataGridTextColumn();
            dgtc1.Header = "First Name";
            dgtc1.Binding = new Binding("FirstName");
            dgtc1.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
            dataGrid.Columns.Add(dgtc1);

            DataGridTextColumn dgtc2 = new DataGridTextColumn();
            dgtc2.Header = "Zip Code";
            dgtc2.Binding = new Binding("ZipCode");
            dgtc2.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
            dgtc2.CopyingCellClipboardContent += new EventHandler<DataGridCellClipboardEventArgs>(dgtc2_CopyingCellClipboardContent);
            dataGrid.Columns.Add(dgtc2);
        }

        void dgtc2_CopyingCellClipboardContent(object sender, DataGridCellClipboardEventArgs e)
        {
            DataGridTextColumn dgtc = sender as DataGridTextColumn;
            dgtc.SetValue(dgtc.GetValue() + "-0000"); //ERROR
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }

    }
}
  • 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-13T01:28:52+00:00Added an answer on May 13, 2026 at 1:28 am

    Pls, see if code below would work fine for you. I’ve made an DataGridTextColumn descendant class with overriden GenerateElement method. There you could manipulate with the TextBlock control created for the given cell, e.g alter its value to add extra digits for zip codes.

    ...
    DataGridTextColumn dgtc2 = new ExtendedDataGridTextColumn(); 
    dgtc2.Header = "Zip Code"; 
    ...
    
    public class ExtendedDataGridTextColumn : DataGridTextColumn 
    {
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            TextBlock element = (TextBlock)base.GenerateElement(cell, dataItem);
            element.Text = ((Customer)dataItem).ZipCode + "-0000";
            return element;
        }
    }
    

    hope this helps,
    regards

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

Sidebar

Related Questions

When I run the following Northwind WPF Toolkit Datagrid code from this article ,
I am using following code in WPF to display image in webcam usingEmguCv library
I have following code in WPF XAML and want it to be converted to
I'm using the following code to display unhandled exceptions in a WPF application: public
When I display a JPEG in my WPF application (using the following code), it
The following ToolTip code works in WPF . I'm trying to get it to
I am using following code to get source for image in wpf (image is
Im trying to load a image at runtime in WPF using the following code
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I am trying my hands on WPF MVVM. I have written following code in

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.