title: Dynamically bind the DataGridComboBoxColumn in Code behind
body: <p>I have a datagrid with some datagridcombobox column in it,I am setting the binding source of combobox column in xaml as Static Resource.But the problem is I dont know how to rebind the itemsSource to get the latest changes that I made to the ItemSource .</p>
  
  <p>My xaml is:</p>
  
  <pre><code>&lt;Grid &gt;
      &lt;Grid.Resources&gt;
          &lt;ObjectDataProvider x:Key="ProductDataProvider" ObjectType="{x:Type local:clsPurchaseOrderList}" MethodName="GetProducts" /&gt;
      &lt;/Grid.Resources&gt;
     &lt;my:DataGrid Name="dgvPurchaseOrder"
                               ItemsSource="{Binding}" 
                               SelectionUnit="CellOrRowHeader"
                               TabIndex="3"&gt;
                      &lt;my:DataGrid.Columns&gt;
  
                          &lt;my:DataGridComboBoxColumn 
                                         Width="100"
                                         Header="Product Code"
                                         SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"                                                                                       
                                         SelectedValuePath="Product_Id"
                                         DisplayMemberPath="Product_Code"                                           
                                         ItemsSource="{Binding Source={StaticResource ProductDataProvider}}"&gt;
                              &lt;my:DataGridComboBoxColumn.EditingElementStyle&gt;
                                  &lt;Style TargetType="ComboBox"&gt;
                                      &lt;Setter Property="IsEditable" Value="True" /&gt;
                                  &lt;/Style&gt;
                              &lt;/my:DataGridComboBoxColumn.EditingElementStyle&gt;
                          &lt;/my:DataGridComboBoxColumn&gt;
                                     .
                                     .
                                     .
                      &lt;/my:DataGrid.Columns&gt;
                  &lt;/my:DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>My code behind is:</p>
  
  <pre><code>class clsPurchaseOrderList : INotifyPropertyChanged, IDataErrorInfo
  {
      //Constructor
      public clsPurchaseOrderList()
      {
          GetProducts();
      }
     private int _Product_Id;
      public int Product_Id
      {
          get { return _Product_Id; }
          set
          {
              _Product_Id = value;
              OnPropertyChanged("Product_Id");
          }
      }
  
     //Method
      public DataView GetProducts()
      {
          DataSet ds = new DataSet();
          string qry = "select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PM.Is_Del=0 and PM.Is_Active=1";
          ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry);
          if (ds.Tables[0].Rows.Count &gt; 0)
              return ds.Tables[0].DefaultView;
          else
              return null;
      }
  
  }
  </code></pre>
  
  <p>My problem is how do I bind the DataGridComboBoxColumn in code behind instead of in  xaml as static resource hence I can bind the ItemSource as Dynamically by calling the GetProducts() method?</p>
  
  <p>Edit:</p>
  
  <p>My business Object is:</p>
  
  <pre><code>class clsPurchaseOrderList : INotifyPropertyChanged, IDataErrorInfo
  {
      public clsPurchaseOrderList()
      {
          GetProducts();
      }
  
     private int _Product_Id;
     private ObservableCollection&lt;Products&gt; testCollection;
  
      public int Product_Id
      {
          get { return _Product_Id; }
          set
          {
              _Product_Id = value;
              OnPropertyChanged("Product_Id");
          }
      }
  
      public ObservableCollection&lt;Products&gt; TestCollection
      {
          get
          {
              return this.testCollection;
          }
      }
  
      public void GetProducts()
      {
          DataSet ds = new DataSet();
          DataTable testTable = new DataTable();
          string qry = "select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PM.Is_Del=0 and PM.Is_Active=1";
          ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry);
  
          testTable = ds.Tables[0];
  
          testCollection = new ObservableCollection&lt;Products&gt;();
  
          foreach (DataRow row in testTable.Rows)
          {
              var obj = new Products()
              {
                  Product_Id = (int)row["Product_Id"],
                  Product_Code = (string)row["Product_Code"],
                  Product_Name = (string)row["Product_Name"],
                  Product_Type = (string)row["Product_Type"]
              };
              testCollection.Add(obj);
          }
  
          this.OnPropertyChanged("TestCollection");
      }
   }
  </code></pre>
  
  <p>My xaml is :</p>
  
  <pre><code>&lt;UserControl x:Class="RH_Inventory_Management_System.PURCHASING.Purchase_Order"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
     &lt;Grid&gt;
       &lt;my:DataGrid Name="dgvPurchaseOrder"
                           ItemsSource="{Binding}" 
                           SelectionUnit="CellOrRowHeader"
                           TabIndex="3"&gt;
                  &lt;my:DataGrid.Columns&gt;
  
                      &lt;my:DataGridComboBoxColumn 
                                     Width="100"
                                     Header="Product Code"
                                     SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"                                                                                       
                                     SelectedValuePath="Product_Id"
                                     DisplayMemberPath="Product_Code"                                           
                                     ItemsSource="{Binding Path=TestCollection,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}"&gt;
                          &lt;my:DataGridComboBoxColumn.EditingElementStyle&gt;
                              &lt;Style TargetType="ComboBox"&gt;
                                  &lt;Setter Property="IsEditable" Value="True" /&gt;
                              &lt;/Style&gt;
                          &lt;/my:DataGridComboBoxColumn.EditingElementStyle&gt;
                      &lt;/my:DataGridComboBoxColumn&gt;
                                 .
                                 .
                                 .
                  &lt;/my:DataGrid.Columns&gt;
              &lt;/my:DataGrid&gt;
     &lt;/Grid&gt;
  </code></pre>
  
  <p>My code behind is:</p>
  
  <pre><code>public partial class Purchase_Order : UserControl
  {
     ObservableCollection&lt;clsPurchaseOrderList&gt; lstItems;
     public Purchase_Order()
      {
          InitializeComponent();
          lstItems = new ObservableCollection&lt;clsPurchaseOrderList&gt;();
          dgvPurchaseOrder.ItemsSource = lstItems;
      }
   }
  </code></pre>