Link to home
Start Free TrialLog in
Avatar of Kevin Robinson
Kevin Robinson

asked on

SIlverlight Combobox Datagrid

I have a combo boxs in the data grid below. It lists the look table contents fine but when I select an option from the combo box all the records get the same value.  I can change the description fine so that the description is different for each record.

so for example if i change a project code from Core to VBI in the table below.

Description         ProjectCode
-------------------------------------------
Test 1 xxx          Core
Test 2 xxx          VBI
Test 3 xxx          MV


I Get this
Description         ProjectCode
-------------------------------------------
Test 1 xxx          VBI
Test 2 xxx          VBI
Test 3 xxx          VBI

                 
<my:DataGrid Name="dgMonday"  HorizontalAlignment="Left"  
                                 AutoGenerateColumns="False" Height="200" Width="825" IsReadOnly="False"  
                                 ItemsSource="{Binding Data, ElementName=TimeSheetsDataSource}" >
                    <my:DataGrid.Columns>
                                    
                                    <my:DataGridTextColumn Header="Description" Width="300" Binding="{Binding DescrTask}" />

                                    <my:DataGridTemplateColumn Header="Project">
                                        <my:DataGridTemplateColumn.CellTemplate >
                                            <DataTemplate>
                                                <StackPanel>
                                                    <ComboBox 
                                                         ItemsSource="{Binding Path=Data, Source={StaticResource ProjectTypeDataSource}}"
                                                        SelectedItem="{Binding Path=SelectOption, Mode=TwoWay}" 
                                                        HorizontalAlignment="Left"
                                                        DisplayMemberPath="ProjectCode"   
                                                        SelectedValuePath="ProjectId"  Width="100"
                                                        SelectedValue="{Binding ProjectId, Mode=TwoWay}" Margin="0"/>
                                                </StackPanel>
                                            </DataTemplate>
                                        </my:DataGridTemplateColumn.CellTemplate>
                                    </my:DataGridTemplateColumn>

                                 
                                    <my:DataGridTextColumn Header="UserName" Width="50" Binding="{Binding UserName}" />
                                </my:DataGrid.Columns>
                            </my:DataGrid>

Open in new window

Avatar of Ashok
Ashok
Flag of United States of America image

Try this.....

                <my:DataGridTextColumn Header="Description" Width="300" Binding="{Binding DescrTask}" />

                <my:DataGridTemplateColumn Header="Project">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ProjectCode}" />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                    <my:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox SelectedItem="{Binding Path=SelectOption, Mode=TwoWay}"
                                      ItemsSource="{Binding Path=Data, Source={StaticResource ProjectTypeDataSource}}"
                                      DisplayMemberPath="ProjectCode"
                                  />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellEditingTemplate>
                </my:DataGridTemplateColumn>

                <my:DataGridTextColumn Header="UserName" Width="50" Binding="{Binding UserName}" />

HTH
Ashok
Avatar of Kevin Robinson
Kevin Robinson

ASKER

Actually adding the datasource to within the template seems to fix the problem.  

Binding to a static resouce binding source to each row obviously links them all to the same source.

Any Comments?

<my:DataGridTemplateColumn Header="Project">
                      <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel >
                                    <riaControls:DomainDataSource Name="ProjectTypes" AutoLoad="True" QueryName="GetProjects">
                                        <riaControls:DomainDataSource.DomainContext>
                                            <ds:TimeSheetServiceContext  />
                                        </riaControls:DomainDataSource.DomainContext>
                                    </riaControls:DomainDataSource>
                                    <ComboBox  ItemsSource="{Binding ElementName=ProjectTypes, Path=Data}"
                                        DisplayMemberPath="ProjectCode" 
                                        SelectedItem="{Binding ProjectId,Mode=TwoWay}"/>
                                </StackPanel>
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
                    </my:DataGridTemplateColumn>

Open in new window

I cannot exactly follow your data setup.
But, you can look at complete example at
http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx

and learn from it and apply in your situation.

HTH
Ashok
Well the Domain Datasource bit was in the resources section of the page. The combo box was bound in the same way but pointed to the resouce section. It filled the combobox ok but had the everything connected issue. i.e change one they all changed. I even tried setting the "Mode=On Time" option but no difference.

This WAS in the User Control Resources Section
<?xml:namespace prefix = riaControls /> <UserControl.Resources>
<riaControls:DomainDataSource Name="ProjectTypes" AutoLoad="True" QueryName="GetProjects">
        <riaControls:DomainDataSource.DomainContext>
                                            <ds:TimeSheetServiceContext  />
          </riaControls:DomainDataSource.DomainContext>
 </riaControls:DomainDataSource>

 </UserControl.Resources>                                    

Instead on inside the Data Ttemplate where it is now (solving the issue)
 <DataTemplate>
      <StackPanel >
              <riaControls:DomainDataSource Name="ProjectTypes" AutoLoad="True"        QueryName="GetProjects">
                   <riaControls:DomainDataSource.DomainContext>
                             <ds:TimeSheetServiceContext  />
                     </riaControls:DomainDataSource.DomainContext>
            </riaControls:DomainDataSource>

             <ComboBox  ItemsSource="{Binding ElementName=ProjectTypes, Path=Data}"
                                        DisplayMemberPath="ProjectCode"
                                        SelectedItem="{Binding ProjectId,Mode=TwoWay}"/>
        </StackPanel>
</DataTemplate>


<?xml:namespace prefix = ds />


 

Well the Domain Datasource bit was in the resources section of the page. The combo box was bound in the same way but pointed to the resouce section. It filled the combobox ok but had the everything connected issue. i.e change one they all changed. I even tried setting the "Mode=On Time" option but no difference.




 

<UserControl.Resources>
<riaControls:DomainDataSource QueryName="GetProjects" AutoLoad="True" Name="ProjectTypes"><riaControls:DomainDataSource Name="ProjectTypes" AutoLoad="True" QueryName="GetProjects">
       <riaControls:DomainDataSource.DomainContext>
               <ds:TimeSheetServiceContext  />
         </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
</UserControl.Resources>        

Open in new window

this fixed the issue

Instead on inside the Data Ttemplate where it is now (solving the issue)
<DataTemplate>
     <StackPanel >
               <riaControls:DomainDataSource Name="ProjectTypes" AutoLoad="True"        QueryName="GetProjects">
                  <riaControls:DomainDataSource.DomainContext>
                            <ds:TimeSheetServiceContext  />                    </riaControls:DomainDataSource.DomainContext>          </riaControls:DomainDataSource></riaControls:DomainDataSource.DomainContext></riaControls:DomainDataSource>
<riaControls:DomainDataSource QueryName="GetProjects" AutoLoad="True" Name="ProjectTypes"><riaControls:DomainDataSource.DomainContext>             <ComboBox  ItemsSource="{Binding ElementName=ProjectTypes, Path=Data}"
         DisplayMemberPath="ProjectCode" 
         SelectedItem="{Binding ProjectId,Mode=TwoWay}"/>
       </StackPanel>
</DataTemplate>

Open in new window

You have to use on Datasource for all fields in DataGrid to DISPLAY
and
you have to use another Datasource to populate ComboBox.

If you use only one Datasource, you will see same value in all ComboBox.

Hope this makes sense.

Ashok
If you are using 2 different Datasource, you must have some issue with how you are coding ComboBox.

Ashok
ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad to see you got it fixed.

Ashok