Wednesday, October 17, 2012

Datagrid in Silverlight

Datagrid in Silverlight

I have a datagrid, each row lists a product and it's suggested reorder quantity (populated via server side code) I'd like the end user to over-write the suggested quantity, this value should be typed into the datagrid REPLACING the old suggested quantity.. The issue is that I cannot for the life of me commit the changes to the cell edit and allow me to change row's again.

In short, when I finish typing into the column and hit "enter" i am unable to escape the row..

Here's my XAML

<sdk:DataGrid Name="ReorderSuggestionsDataGrid"               AutoGenerateColumns="False"               SelectionChanged="ReorderSuggestionsDataGrid_SelectionChanged"               CellEditEnding="ReorderSuggestionsDataGrid_CellEditEnding"               CellEditEnded="ReorderSuggestionsDataGrid_CellEditEnded"               Grid.Row="1">      <sdk:DataGrid.Columns>         <sdk:DataGridTextColumn x:Name="ProductIDColumn"                                 Header="Product"                                 Width="180"                                 IsReadOnly="true"                                 Binding="{Binding Path=Product.Name, Mode=OneWay}" />         <sdk:DataGridTextColumn x:Name="QtySuggestedColumn"                                 Header="Qty"                                 Width="65"                                 Binding="{Binding Path=QuantitySuggested, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />     </sdk:DataGrid.Columns> </sdk:DataGrid> 

and here's my codebehind function...

private void ReorderSuggestionsDataGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e) {     try     {         PurchaseOrderDetail stuff = (PurchaseOrderDetail)ReorderSuggestionsDataGrid.SelectedItem;         context.PurchaseOrderDetails                .Where(pod => pod.PurchaseOrderDetailID == stuff.PurchaseOrderDetailID)                .First()                .QuantityOrdered = stuff.QuantitySuggested;         ReorderSuggestionsDataGrid.CommitEdit();     }      catch (Exception z)     {     } } 

I've tried the ReorderSuggestionsDataGrid_CellEditEnded as well as Editing... I've also tried the updatesourcetrigger, commitedit(), canceledits()...

For sure when I debug and step through, there's a flag in the Datagrid properties (IsValid) that indicates its invalid. But I can't figure out what's making it invalid and ultimately how to commit the changes to the context (without updating the DBase) and then refreshing the list to show the new changed values. Thanks in advance!

Answers & Comments...

Answer: 1

Hi this is the common problem we get in silverlight datagrid, (i).You have to use Mode=TwoWay. (ii).you need to assign the itemssource to the data grid once you complete editing , hope this will work.

ReorderSuggestionsDataGrid.ItemsSource = Your edited List(); 
by : Sajeetharanhttp://stackoverflow.com/users/1749403




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog