L.E.
L.E.

Reputation: 645

WPF DataGrid with cell style -- different cell style in same column

I was just wondering how can I assign different cell style for same column? Cell style might be combo box or text box. Image uploaded. Is it really hard?

I want to display a table in data grid. The first column should be name of the table field, second column should be the value of the column. now if the first column cell data type is var char , then second column cell should display text box. if the first column cell data type is int, then second column cell should display combo box.

DataGrid Cell Style

Thanks, N avatar

alt text

Upvotes: 1

Views: 2281

Answers (2)

Aran Mulholland
Aran Mulholland

Reputation: 23935

You could :

  • Derive from DataGridBoundColumn (or one of its derivatives) and override the

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)

method. This method creates the item that appears in each cell. The dataItem passed as a parameter is the item in the row. The hassle is that to decide what item you use to display the current cell value you will have to perform the binding manually to get the item to display in the cell and then see what ui control to return.

  • Use a Template Column and in the cell template use data triggers to swap in the ui element that you wish to display.

Upvotes: 1

Lane
Lane

Reputation: 2719

When asking this question, keep in mind that there are two different kinds of WPF datagrids. The one released on CodePlex in the WPFToolkit which uses .NET Framework 3.5 SP1. And the newer WPF DataGrid released as a built-in control in WP4 which uses .NET Framework 4.0.

Your solution may differ depending on which datagrid you decide to use, and its possible you may find one to work better than the other.

That being said, I found an article on C# Corner that describes how to display a combobox in a cell when the cell is in edit mode. This may help you. If you start here, you may find that you can extend this code sample to fit your needs. And in the cases where you need to use a textbox instead of a combobox when editing, you could probably set the combobox to allow typing in new values, essentially letting the user type in values in a textbox when the item they want is not available. Not sure if this is what you want, but I at least wanted to give you something to chew on.

Good luck,

Upvotes: 0

Related Questions