user3428422
user3428422

Reputation: 4570

WPF DataGrid User Control - adding a new column in a view

I have a user control that has a Datagrid that has 3 columns. I want to use this user control in a view but I need to add one more column to the Datagrid but only in this view.

Is this possible?

Code on the view

// Initialising the Usercontrol on the new view
xmlns:myuct="clr-namespace:Customer.UserControls">
<Grid>
    <myuct:CustomerSearch x:Name="CS"/>
</Grid>

This shows the Datagrid and other items of the user control in the view correctly.

Any help would be grateful.

Upvotes: 0

Views: 334

Answers (2)

techhero
techhero

Reputation: 1114

I will suggest that you make your usercontrol accept a flag or switch that can show and hide the additional column. Use DependencyProperties so that you can set the flag/switch in XAML.

// Initialising the Usercontrol on the new view
xmlns:myuct="clr-namespace:Customer.UserControls">
<Grid>
    <myuct:CustomerSearch x:Name="CS" ShowAddOnColumn="true"/>
</Grid>

Upvotes: 2

Ricardo Romo
Ricardo Romo

Reputation: 1624

To programatically add a column:

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "First Name"; 
textColumn.Binding = new Binding("FirstName"); 
dataGrid.Columns.Add(textColumn); 

Upvotes: 0

Related Questions