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

[Solved] Setting SelectedItem of ComboBox after ItemSource is set in GridView

1 Answer 177 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.
James Steward
Top achievements
Rank 1
James Steward asked on 09 Jul 2009, 04:30 AM
Hi,
I'm trying to set the SelectedItem on 3 ComboBoxs that are in a ControlTemplate in in a GridView Column. The ComboBoxes are getting bound to their ItemsSources during the ComboBoxes Loaded event. I would like to set the SelectedItem for the ComboBoxes based on values bound to the GridViewRow.

<grid:GridViewDataColumn HeaderText="">  
                    <grid:GridViewColumn.CellStyle> 
                        <Style TargetType="gridView:GridViewCell">  
                            <Setter Property="Template">  
                                <Setter.Value> 
                                    <ControlTemplate TargetType="gridView:GridViewCell">  
                                        <StackPanel Orientation="Vertical">  
                                            <TextBlock>Stage:</TextBlock> 
                                            <ComboBox x:Name="cbStage" SelectedItem="{Binding Stage}" Loaded="cbStage_Loaded" /> 
                                            <TextBlock>Advisor:</TextBlock> 
                                            <!--ComboBox x:Name="cbAdvisor" /--> 
                                            <TextBlock>Sponsor:</TextBlock> 
                                            <!--ComboBox x:Name="cbSponsor" /--> 
                                        </StackPanel> 
                                    </ControlTemplate> 
                                </Setter.Value> 
                            </Setter> 
                        </Style> 
                    </grid:GridViewColumn.CellStyle> 
                </grid:GridViewDataColumn> 

 private void cbStage_Loaded(object sender, RoutedEventArgs e)  
        {  
            ComboBox cb = (ComboBox)sender;  
            if (stageListData != null)  
            {  
                string[] source = new string[stageListData.Count + 1];  
                source[0] = "No Stage";  
                for (int i = 0; i < stageListData.Count; i++)  
                {  
                    source[i + 1] = stageListData[i].StageName;  
                }  
                cb.ItemsSource = source;  
            }  
            else 
            {  
                string[] source = new string[1];  
                source[0] = "No Stage";  
                cb.ItemsSource = source;  
            }  
        } 

I thought I might be able to do it using the RowLoaded event but I can't seem to figure out how to access the ComboBoxes from there.
Any help would be greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
James Steward
Top achievements
Rank 1
answered on 10 Jul 2009, 04:19 PM
Nevermind, I figured it out. I can take care of it all in the RowLoaded event:

<grid:GridViewDataColumn HeaderText="">  
                    <grid:GridViewColumn.CellStyle> 
                        <Style TargetType="gridView:GridViewCell">  
                            <Setter Property="Template">  
                                <Setter.Value> 
                                    <ControlTemplate TargetType="gridView:GridViewCell">  
                                        <StackPanel Orientation="Vertical">  
                                            <TextBlock>Stage:</TextBlock> 
                                            <ComboBox x:Name="cbStage" /> 
                                            <TextBlock>Advisor:</TextBlock> 
                                            <TextBox Text="{Binding Advisor}" /> 
                                            <TextBlock>Sponsor:</TextBlock> 
                                            <TextBox Text="{Binding Sponsor}" /> 
                                        </StackPanel> 
                                    </ControlTemplate> 
                                </Setter.Value> 
                            </Setter> 
                        </Style> 
                    </grid:GridViewColumn.CellStyle> 
                </grid:GridViewDataColumn> 

void RadGridView1_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)  
        {  
            GridViewRow row = e.Row as GridViewRow;  
            if (row != null && row.Items.Count > 6)  
            {  
                GridViewCell cell = row.Items[7] as GridViewCell;  
                if (cell != null)  
                {  
                    var comboStage = cell.ChildrenOfType<ComboBox>().Where(b => b.Name == "cbStage").FirstOrDefault();  
                    ComboBox cbStage = (ComboBox)comboStage;  
                    if (stageListData != null)  
                    {  
                        string[] source = new string[stageListData.Count + 1];  
                        source[0] = "No Stage";  
                        for (int i = 0; i < stageListData.Count; i++)  
                        {  
                            source[i + 1] = stageListData[i].StageName;  
                        }  
                        cbStage.ItemsSource = source;  
                    }  
                    else  
                    {  
                        string[] source = new string[1];  
                        source[0] = "No Stage";  
                        cbStage.ItemsSource = source;  
                    }  
                    var record = e.Row.Record as DataRecord;  
                    IdeaListData data = (IdeaListData)record.Data;  
                    string stage = data.Stage;  
                    cbStage.SelectedItem = stage;  
                }  
            }  
        } 
Tags
GridView
Asked by
James Steward
Top achievements
Rank 1
Answers by
James Steward
Top achievements
Rank 1
Share this question
or