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

Dynamic ItemsSource for RadComboBox in a programmatically created GridView

2 Answers 392 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philipp Egger
Top achievements
Rank 1
Philipp Egger asked on 03 Apr 2011, 10:51 AM

Dear Experts,

I have a RadGridView which is programmatically created. In a column I have a RadComboBox.

The ItemsSource of the ComboBox is dynamic per line. So it's content will change per line depending on another column's field value. So it is NOT a autocomplete functionality in which the dropdown is created while you type. It is created while you open the list.

At the moment, I am using the DropDownOpened to assign a new ItemsSource to the ComboBox.

Problem:

The dropdown shows fine, but the values are not written back to the column field once you select an entry and the combo is unfocused. See screenshots: dynamic-??.PNG.

I have tried to use different BindingModes (has to be programmatically) - unfortunately without success.

Thanks for your feedback.

Best Regards,

Phil

Here is the code:

<UserControl.Resources>
    <DataTemplate x:Key="cellEditTemplateDrp">
        <telerik:RadComboBox Name="cmbBox" DropDownOpened="cmbBox_DropDownOpened">
            <telerik:RadComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding ID}" Margin="0,0,5,0" Width="30" />
                        <TextBlock Text="{Binding Description}" />
                    </StackPanel>
                </DataTemplate>
            </telerik:RadComboBox.ItemTemplate>
        </telerik:RadComboBox>
    </DataTemplate>
</UserControl.Resources>
  
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
    <telerik:RadGridView HorizontalAlignment="Left" Name="radGridView2" VerticalAlignment="Top" AutoGenerateColumns="False" />
</Grid>
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }
    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
        // get this data dynamically from an external source (not a database)
        DataTable table = new DataTable(); 
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "ID", DataType = typeof(int) }); 
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "Name", DataType = typeof(string) });
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "InputString1", DataType = typeof(string) });
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "InputString2", DataType = typeof(string) });
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "InputString3", DataType = typeof(string) }); 
          
        for (var i = 0; i < 1000; i++) 
        
            DataRow row = new DataRow(); 
            row["ID"] = i; 
            row["Name"] = "Name " + i.ToString();
            row["InputString1"] = i.ToString();
            row["InputString2"] = i.ToString();
            row["InputString3"] = "1"; 
            table.Rows.Add(row); 
        }
        // generate columns
        this.GenerateDetailColumns(table);
        // assign source
        this.radGridView2.ItemsSource = table;
    }
    private void GenerateDetailColumns(DataTable list)
    {
        this.radGridView2.Columns.Clear();
        foreach (DataColumn column in list.Columns)
        {
            GridViewDataColumn col = new GridViewDataColumn();
            col.UniqueName = column.ColumnName;
            col.Header = column.ColumnName;
            if(column.ColumnName == "InputString3")  // dynamically set combo
            {
                col.CellEditTemplate = Resources["cellEditTemplateDrp"] as DataTemplate;
            }
            this.radGridView2.Columns.Add(col);
        }
    }
    private void cmbBox_DropDownOpened(object sender, EventArgs e)
    {
        RadComboBox cmb = sender as RadComboBox;
        DataTable table = new DataTable();
        table.Columns.Add(new DataColumn()
                    { ColumnName = "ID", DataType = typeof(string) });
        table.Columns.Add(new DataColumn() 
                    { ColumnName = "Description", DataType = typeof(string) });
        for (var i = 0; i < 10; i++)
        {
            DataRow row = new DataRow();
            row["ID"] = i.ToString();
            row["Description"] = "Description " + i.ToString();
            table.Rows.Add(row);
        }
        cmb.ItemsSource = table.ToList();
        /*
        // this binding does not resolve the problem. it ends up with an empty dropdownlist
        System.Windows.Data.Binding bind = new System.Windows.Data.Binding();
        bind.Mode = System.Windows.Data.BindingMode.OneWay;
        cmb.SetBinding(RadComboBox.ItemsSourceProperty, bind);
        System.Windows.Data.Binding bind2 = new System.Windows.Data.Binding("ID");
        bind2.Mode = System.Windows.Data.BindingMode.TwoWay;
        cmb.SetBinding(RadComboBox.SelectedValueProperty, bind);
        */
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 06 Apr 2011, 02:27 PM
Hi Philipp Egger,

I have updated the project from this blog post so that it suits the needs described here. May you take a look at the sample attached and let me know whether it meets your requirements ? 

Best wishes,
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
Philipp Egger
Top achievements
Rank 1
answered on 06 Apr 2011, 08:52 PM

Dear Maya,

Thanks a lot for your valued input.

Actually, I was missing these lines:

System.Windows.Data.Binding bind = new System.Windows.Data.Binding("InputString3"); 
bind.Mode = System.Windows.Data.BindingMode.TwoWay; 
cmb.SetBinding(RadComboBox.SelectedValueProperty, bind); 
cmb.SelectedValuePath = "ID"; 

This is equal to the SelectedValue and SelectedValuePath in the XAML that you posted:

<telerik:RadComboBox ... SelectedValue="{Binding InputString3, Mode=TwoWay}" SelectedValuePath="ID">

So, my working function looks like this now:

private void cmbBox_DropDownOpened(object sender, EventArgs e)
{
    RadComboBox cmb = sender as RadComboBox;
    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn() { 
                ColumnName = "ID", DataType = typeof(string) });
    table.Columns.Add(new DataColumn() { 
                ColumnName = "Description", DataType = typeof(string) });
    for (var i = 0; i < 10; i++)
    {
        DataRow row = new DataRow();
        row["ID"] = i.ToString();
        row["Description"] = "Description " + i.ToString() + 
                 " (" + DateTime.Now.Ticks.ToString() + ")";
        table.Rows.Add(row);
    }
    // SelectedValue="{Binding InputString3, Mode=TwoWay}" SelectedValuePath="ID"
    System.Windows.Data.Binding bind = new System.Windows.Data.Binding("InputString3");
    bind.Mode = System.Windows.Data.BindingMode.TwoWay;
    cmb.SetBinding(RadComboBox.SelectedValueProperty, bind);
    cmb.SelectedValuePath = "ID";
    cmb.ItemsSource = table.ToList();
}

Best Regards,

Phil
Tags
GridView
Asked by
Philipp Egger
Top achievements
Rank 1
Answers by
Maya
Telerik team
Philipp Egger
Top achievements
Rank 1
Share this question
or