We help IT Professionals succeed at work.

SIlverlight Combobox Datagrid

1,465 Views
Last Modified: 2013-11-12
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

Comment
Watch Question

AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
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
Kevin RobinsonPrivate VB.NET Contractor

Author

Commented:
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

AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
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
Kevin RobinsonPrivate VB.NET Contractor

Author

Commented:
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 />


 
Kevin RobinsonPrivate VB.NET Contractor

Author

Commented:

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

Kevin RobinsonPrivate VB.NET Contractor

Author

Commented:
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

AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
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
AshokSr. Software Engineer
CERTIFIED EXPERT

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

Ashok
Sr. Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Glad to see you got it fixed.

Ashok

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.