Click here to Skip to main content
15,896,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the WPF datagrid, I have added the columns DataGridComboBoxColumn, DataGridTemplateColumn programatically.

I want every row (every comboBox) of the the column with the different datasource may be first row’s dropdown will have 5 items and second row’s dropdown will have 3 items.


C#
DataGridComboBoxColumn colcombo = new DataGridComboBoxColumn();
colcombo.Header = "FirstName";
dataGrid1.Columns.Add(colcombo);
         //===================================================================
DataGridTemplateColumn cbColcmbBox = new DataGridTemplateColumn();           
cbColcmbBox.Header = "LastNamecombo";
FrameworkElementFactory factorycombo = new 	FrameworkElementFactory(typeof(ComboBox));
DataTemplate cellTemplatecomb = new DataTemplate();
cellTemplatecomb.VisualTree = factorycombo;
cbColcmbBox.CellTemplate = cellTemplatecomb;            
dataGrid1.Columns.Add(cbColcmbBox);


Is this the correct way to add the DataGridTemplateColumn , if I want to access this programatically ?
Posted
Updated 23-Jul-10 2:46am
v2
Comments
Sandeep Mewara 23-Jul-10 8:46am    
Always use PRE tags to format the code part. It makes the question readable

If you want to add every row of your datagrid with multiple datasource dropdown then above logic want work, then you have to write code on rowload event.

Take a tempate column with blank stackpannel and in rowaoad event take new dropdown with different databasource and add it in relavnt stackpannel.

C++
stkpnl.child.add(objdropdown);


Thanks,
Amit Patel
 
Share this answer
 
v2
Comments
farookbe2006 27-Jul-10 3:23am    
Hi Amit thanks for ur ans, I have tried following but not working ..plz check the code ------------added resource --------------- --------added column-------------------------------------------- DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn(); dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star); dgTemplateColumn.Header = "Manage Options"; dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate; dataGrid1.Columns.Add(dgTemplateColumn); ------------------- void dataGrid1_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e) { DataTemplate dt = (DataTemplate)e.Row.FindResource("manageAreaCellTemplate"); object stk = dt.LoadContent(); StackPanel stk1 = new StackPanel(); stk1 =(StackPanel)stk; ComboBox cmb = new ComboBox(); PortInfoCollection portInfoCollection = new PortInfoCollection(); cmb.ItemsSource = portInfoCollection; cmb.SelectedValuePath = "Visits"; cmb.DisplayMemberPath = "Port"; stk1.Children.Add(cmb); } combo box is not getting populated..in the grid
Hi thanks for ur ans, I have tried following but not working ..plz check the code ------------added resource --------------- --------added column-------------------------------------------- DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn(); dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star); dgTemplateColumn.Header = "Manage Options"; dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate; dataGrid1.Columns.Add(dgTemplateColumn); ------------------- void dataGrid1_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e) { DataTemplate dt = (DataTemplate)e.Row.FindResource("manageAreaCellTemplate"); object stk = dt.LoadContent(); StackPanel stk1 = new StackPanel(); stk1 =(StackPanel)stk; ComboBox cmb = new ComboBox(); PortInfoCollection portInfoCollection = new PortInfoCollection(); cmb.ItemsSource = portInfoCollection; cmb.SelectedValuePath = "Visits"; cmb.DisplayMemberPath = "Port"; stk1.Children.Add(cmb); } combo box is not getting populated..in the grid
 
Share this answer
 

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