Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How a use Add DataGridComboBoxColumn in wpf DataGrid at Runtime.
User Specify total number of columns at runtime. According to runtime input the colunm should be created dynamically. Here i'm not able to add new row.

What I have tried:

XAML:
XML
DataGrid x:name="dataGrid"

c#
C#
DataGridTextColumn so = new DataGridTextColumn();
so.Header = "S.No";
dataGrid.Columns.Add(so);

DataGridTextColumn name= new DataGridTextColumn();
name.Header = " Name";
dataGrid.Columns.Add(name);

DataGridColumn age= new DataGridTextColumn();
age.Header = "Age";
dataGrid.Columns.Add(age);

for (int i = 1; i <= p; i++)
{
    DataGridComboBoxColumn ss = new DataGridComboBoxColumn();
    ss.Header = i;
    List<string> list = new List<string>();
    list.Add("Item 1");
    list.Add("Item 2");
    ss.ItemsSource = list;
    ss.Width = 50;
    ss.EditingElementStyle = style;

    dataGrid.Columns.Add(ss);
}
Posted
Updated 18-Mar-16 0:41am
v4
Comments
VR Karthikeyan 18-Mar-16 6:34am    
Point out in which line the problem occurs?

1 solution

Hi, your code is working fine for me, However, after the selection, the selected item is not sets in the current cell. Follow the below link, this is the best way to add combobox column in wpf datagrid.
ComboBox in DataGrid in WPF[^]
 
Share this answer
 
v2
Comments
Aravindh Aravindhan 18-Mar-16 7:34am    
ya... I'm creating column dynamically(runtime only we know how many columns are required). But it's not there !..
Aravindh Aravindhan 18-Mar-16 7:54am    
Actually the value was sets in combobox. But not displayed inside the cells.How to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900