This is a migrated thread and some comments may be shown as answers.

Define GridView's columns in codebehind

1 Answer 117 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Naseem
Top achievements
Rank 1
Naseem asked on 19 Nov 2010, 05:46 AM
Hi,

I need to define the GridViews's columns in codbehind , because the number of the columns and their binding are dynamic.
Some of these columns are GridViewDataColumn and some are GridViewComboBoxColumn . Would you please send me piece of code that shows how I can define the columns and their features (binding,header,...) in code behind.

My other question is realted to GridViewComboBoxColumn , the data for ComboBox is dynamic , it would be different based on the column . For example the combobox in column one has got some data and the combobox in column2 has got other data. It means I need to handle Loaded event for each combo box as well to load their specific data (when user click on GridViewComboBoxColumn ). I'd be thankful you let me know how I can handle this feature as well.

Here is how I defined the above scenario in standard DataGrid and now I need to replace the standard DaraGrid with TelerikRadGridView

private void createPropertyColumns()
       {
           this.grdProduct.Columns.Clear();
           this.grdProduct.Columns.Add(new DataGridTextColumn { Header = "ProductID", Binding = new Binding("ProductID"), IsReadOnly = true, Width = new DataGridLength(90) });
           this.grdProduct.Columns.Add(new DataGridTextColumn { Header = "SupplierDesc", Binding = new Binding("SupplierDesc"), IsReadOnly = true, Width = new DataGridLength(100) });
           for (int count = 1; count <= ProductFilterCollection.Count; count++)
           {
               ServiceProduct.ProductFilter Filterdesc = ProductFilterCollection.Where(x => x.ProductFilterID == int.Parse(GetFilterID(count, false))).FirstOrDefault();
               DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
               if (Filterdesc != null)
               {
                   templateColumn.Header = Filterdesc.Description.ToString();
               }
               else
               {
                   templateColumn.Header = "";
               }
               templateColumn.CellTemplate = CreateCellTemplate(typeof(TextBlock), "Filter" + count.ToString());
               templateColumn.CellEditingTemplate = (DataTemplate)Resources["myCellEditingTemplate"];
               grdProduct.Columns.Add(templateColumn);
           }
           this.grdProduct.ItemsSource = sp_ProductDetailCollection;
           Cursor = Cursors.Arrow;
       }


<UserControl.Resources>
       <DataTemplate x:Key="myCellEditingTemplate">
           <ComboBox x:Name="ddlProductProperty" Loaded="ddlProductProperty_Loaded"  SelectionChanged="ddlProductProperty_SelectionChanged" ></ComboBox>
       </DataTemplate>
   </UserControl.Resources>

 

private void ddlProductProperty_Loaded(object sender, RoutedEventArgs e)
       {
           foreach (var item in ProductFilterCollection)
           {
               if (grdProduct.CurrentColumn.Header.ToString() == item.Description.ToString())
               {
                   List<ServiceProduct.ProductFilterValue> lstCombo = ProductFilterValueCollection.Where(x => x.ProductFilterID == item.ProductFilterID).ToList();
                   ServiceProduct.ProductFilterValue emptyItem = new ServiceProduct.ProductFilterValue();
                   emptyItem.Value = "<None>";
                   lstCombo.Insert(0, emptyItem);
                   ((ComboBox)sender).ItemsSource = lstCombo;
                   ((ComboBox)sender).DisplayMemberPath = "Value";
               }
           }
       }

Thank you,

1 Answer, 1 is accepted

Sort by
0
Naseem
Top achievements
Rank 1
answered on 22 Nov 2010, 03:47 AM
Thank you, I figure out how to define it in codebehind.
Tags
GridView
Asked by
Naseem
Top achievements
Rank 1
Answers by
Naseem
Top achievements
Rank 1
Share this question
or