Edit this page

Empty Cells in Combobox column

PROBLEM

When you use the GridViewComboBoxColumn you might encounter empty cells in that column:

CAUSE

First you need to check

  • the Output for Binding exceptions

  • if the types are of the same type

If you do not encounter any of the above mentioned problems, then you probably use ElementName binding for that column, e.g.

XAML

<telerik:GridViewComboBoxColumn Header="Category"
DataMemberBinding="{Binding CategoryID}"
ItemsSource="{Binding Path=DataContext.Categories, ElementName=RootElement}"
DisplayMemberPath="CategoryName"
SelectedValueMemberPath="CategoryID" />

This will not work, as the DataContext of the cell would not be the ViewModel, but the business object related to the row instead. We do not recommend such approach.

SOLUTION

There are two ways of solving the issue :

  1. Expose the ViewModel as a static resource on the page so that it can be easily accessible by the binding:

XAML

<UserControl.Resources>
    <local:MyViewModel x:Key="MyViewModel" />
</UserControl.Resources>
  1. Set the ItemsSource of the ComboBox column:

XAML

<telerik:GridViewComboBoxColumn Header="Category"
DataMemberBinding="{Binding CategoryID}"
ItemsSource="{Binding Path=Categories, Source={StaticResource MyViewModel}}"
DisplayMemberPath="CategoryName"
SelectedValueMemberPath="CategoryID" />

C#

private void gridView_DataLoaded(object sender, EventArgs e)
{
    (this.radGridView.Columns["Category"] as GridViewComboBoxColumn).ItemsSource = GetCategories();
}

VB.NET

Private Sub gridView_DataLoaded(ByVal sender As Object, ByVal e As EventArgs)
    TryCast(Me.radGridView.Columns("Category"), GridViewComboBoxColumn).ItemsSource = GetCategories()
End Sub