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

[Solved] Binding GridViewComboBoxColumn at runtime

2 Answers 168 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Scott Waye asked on 07 Oct 2010, 10:11 PM
I have a grid with a GridViewComboBoxColumn column both (grid and dropdown) of which are bound at runtime.  The drop down column only displays the values when it is clicked.  Click in any row in the GridViewComboBoxColumn column and _all_ the rows appear.  Click in another cell not in this column and all the cells are cleared.    Try the code below, and click in the button at the top of the page to bind the grid and see the effect:

Xaml:

<UserControl
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:SL.SelectCheckbox" xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" x:Class="SL.ProgDropDown.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White" >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Content="Bind GridView" Click="Button_Click"></Button>
        <telerik:RadGridView x:Name="SampleGrid" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Code, Mode=TwoWay}" Header="From"
                                                SelectedValueMemberPath="Code" DisplayMemberPath="Name"
                                                ItemsSource="{Binding Path=LookupCodes}"/>
                <telerik:GridViewDataColumn  DataMemberBinding="{Binding Name}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

Code behind:

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
 
namespace SL.ProgDropDown
{
 
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
 
        LookupCode[] codes;
        List<SampleData> items;
 
        public MainPage()
        {
            InitializeComponent();
 
            DataContext = this;
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            items = (from c in Enumerable.Range(0, 10)
                     select new SampleData
                     {
                         Code = "code1",
                         Name = "Some name" + c
                     }).ToList();
 
            codes = new LookupCode[5];
            codes[0] = new LookupCode
                        {
                            Code = "code1",
                            Name = "aaaa"
                        };
            codes[1] = new LookupCode
            {
                Code = "code2",
                Name = "bbbb"
            };
            codes[2] = new LookupCode
            {
                Code = "code3",
                Name = "ccccc"
            };
            codes[3] = new LookupCode
            {
                Code = "code4",
                Name = "ddddd"
            };
            codes[4] = new LookupCode
            {
                Code = "code5",
                Name = "eeeee"
            };
 
            InvokePropertyChanged(new PropertyChangedEventArgs("LookupCodes"));
            InvokePropertyChanged(new PropertyChangedEventArgs("Items"));
        }
 
        public List<SampleData> Items
        {
            get { return items; }
        }
 
        public IEnumerable<LookupCode> LookupCodes
        {
            get { return codes; }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler != null) handler(this, e);
        }
    }
 
    public class LookupCode
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
 
    public class SampleData : INotifyPropertyChanged
    {
        private string code;
        private string name;
 
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
                this.OnPropertyChanged("Code");
            }
        }
 
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                this.OnPropertyChanged("Name");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected virtual void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

This is SL4, using version Telerik 2010.2.924.1040 under IE 8, Windows 7 64bit.

Thanks,

Scott

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 11 Oct 2010, 11:57 AM
Hi Scott Waye,

You may try to set the ItemsSource property of the GridViewComboBoxColumn in the Click event of the button:

private void Button_Click(object sender, RoutedEventArgs e)
{
    items = (from c in Enumerable.Range(0, 5)
             select new SampleData{
             Code = c,
             Name = "Some name" + c
             }).ToList();
    codes = new LookupCode[5];
    codes[0] = new LookupCode{
        CodeID = 1,
        Name = "aaaa"
    };
    codes[1] = new LookupCode{
        CodeID = 2,
        Name = "bbbb"
    };     
    InvokePropertyChanged(new PropertyChangedEventArgs("LookupCodes"));
    InvokePropertyChanged(new PropertyChangedEventArgs("Items"));
    ((GridViewComboBoxColumn)this.SampleGrid.Columns[0]).ItemsSource = this.codes;
}
 

Sincerely yours,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 14 Oct 2010, 07:52 PM
Thanks, that works.  Would be better if it picked it up from the INotifyPropertyChanged, but not a big deal.
Tags
GridView
Asked by
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Maya
Telerik team
Scott Waye
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or