How to set a default value which is bound to another value for the combobox in datagrid

2 posts, 1 answers
  1. Tai
    Tai avatar
    73 posts
    Member since:
    Jun 2010

    Posted 17 Aug 2010 Link to this post

    Hi

    I have a datagrid which has one combobox column.

    -the itemsource of the datagrid will be bound to the data from the database through RIA WCF

    -the itemsource of the combobox which is inside the datagrid will be bound to the ListFinancialAccountType property which is a collection of Financial Account Types (also retrieve through RIA WCF).

    I can show the list of all of the available Account Type Name for the combobox. However, I can't set the default value for the combobox which should be matched with the value of each row in the datagrid

    For example: the value of the combobox will tell us what type of the financial account is(Asset, income,..) . However, its real value in the row of the datagrid is an integer type(FinancialAccountTypeId) but i want it to be displayed as either "Asset", "Income",... So i need to look up on the Financial Account Type List(which will be retrieved through RIA WCF). The FinancialAccountType has FinancialAccountTypeId and AccountTypeName properties.

    So on the rows of the datagrid, the combobox will display "Asset", "Income",....   instead of 1,2,... .

    For my FinancialAccount: properties are

                              AccountDescription  

                              AccountNumber

                             FinancialAccountTypeId              (example: 1, 2,...)

                             FinancialAccountType                   --->navigation property

    For my FinancialAccountType: properties are

                              AccountTypeName                     (example: "Asset", ...)

                              FinancialAccountTypeId              (example: 1,2,...)

                                FinancialAccounts                    ---> navigation property



    This is my XAML code

    <Grid x:Name="LayoutRoot">
            <Grid.Resources>
                <financial:Financial_Account_Types x:Key="FinancialAccountTypes"/>
            </Grid.Resources>
            <sdk:DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left"  Margin="112,83,0,0" Name="financialAccountDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="400">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn x:Name="accountDescriptionColumn" Binding="{Binding Path=AccountDescription}" Header="Account Name" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="accountNumberColumn" Binding="{Binding Path=AccountNumber}" Header="Account Number" Width="SizeToHeader" />
                    <sdk:DataGridTemplateColumn x:Name="financialAccountTypeIdColumn" Header="Account Type" Width="SizeToHeader">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox  ItemsSource="{Binding ListFinancialAccountType, Source={StaticResource FinancialAccountTypes},Mode=TwoWay}"
                                            SelectedValue="{Binding FinancialAccountTypeId, Mode=TwoWay}"
                                           
                                          DisplayMemberPath="AccountTypeName"
                                          SelectedValuePath="FinancialAccountTypeId"
                                          />
                                    
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
             
        </Grid>

    This is my Code Behind

    public partial class Financial_Accounts : Page
        {
            public Financial_Accounts()
            {
                InitializeComponent();
            }
     
            // Executes when the user navigates to this page.
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                FinancialAccountContext financial_ctx = new FinancialAccountContext();
                financial_ctx.Load(financial_ctx.GetFinancialAccountsQuery());
                financialAccountDataGrid.ItemsSource = financial_ctx.FinancialAccounts;
            }
     
            
     
            
     
        }
        public class Financial_Account_Types : ObservableCollection<FinancialAccountType>
        {
            public EntitySet<FinancialAccountType> ListFinancialAccountType
            {
                get
                {
                    FinancialAccountContext financial_ctx = new FinancialAccountContext();
                    financial_ctx.Load(financial_ctx.GetFinancialAccountTypesQuery());
                    return financial_ctx.FinancialAccountTypes;
                }
            }
        }

  2. Answer
    Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 19 Aug 2010 Link to this post

    Hello Tai,

    Thank you for contacting us.

    The code looks OK, and it should work as expected. Attached I am sending you a sample project close to your scenario, which works fine. If you still experience difficulties with the Microsoft's ComboBox please write in the silverlight.net forums. There you will receive more accurate answers to your inquiries.

    Please let us know if you have any other questions regarding our controls.

    Regards,
    Konstantina
    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
  3. DevCraft banner
Back to Top