Issues while using XamWebgrid

Not Answered This post has 0 verified answers | 3 Replies | 2 Followers Thread's RSS feed.

Amit Ranjan
Points 703
Replied On: Sat, Jan 23 2010 4:21 AM Reply
Hi I have few problems while implementing xamwebgrid. I am using VB as code-behind. 1. I need a easy way to bind xamweb grid. I dont find any helpful resource either on your documentation of local samples. I have WCF service returning list, now i want to bind the grid from this list in xaml/vb both but with lesser code. I want it some what this way , like in my earlier WPF apps, i used to create a object dataprovider of the collection/class and then it is called as itemsource for the grid / grid elements. 2.My second problem is that whenever i add new rows to the grid , my values get lost from the grid. Please have a look at the xaml.txt file. Its my xaml snippet of grid. The first three columns of project , milestone and class looses their values. They have textbloxk in their datatemplate and combobox in editor template. I am using here four different collections. The item source of the grid is from collection of class TaskModel which holds Project/Milestones/Class and all other columns While in the Editor template, Project/Milestone and Class dropdown are binded from 3 different lists. While selection they are displaying fine but as soon as they are entered they looses their value. 3. I have few elements out side the grid but these needs to be inserted/updated to db. Help me in achieveing this. 4. I have 7 columns in my grid , i am adding their values and displaying in a dynamically generated column on cell exit edit eevent. But not able to get any method/property to set value in their respective total cell. That is 8th column is of total for 5 rows. so total of row[0]from col[0]to col[7] will be displayed in col[8] which is dynamic and is readonly too. 5. Also when i am adding new row, my new row index show -1 , which throws object refence error as my calculation for totaling starts from 0. Even if i use not nothing condition, it still happening Please , help me asap.
  • Post Points: 32

All Replies

Replied On: Wed, Nov 17 2010 2:50 PM Reply

Hello Amit,

I apologize that no one has yet repsonded to this post. In case you can still use the help you were looking for, I hope to answer all your questions.

I have attached a sample Silverlight project that loosely mimics the XAML you have attached. It utilizes a ViewModel, sample data generated by Expression Blend, and is written in Silverlight 4 and VB. It illustrates how to bind a ViewModel to a XAML UserControl.

1. Binding to the XamGrid is simple. Since you have data being returned from a WCF service, you need to copy the data to a collection that the XamGrid can be bound to. The convention I prefer is to create a ViewModel class. If you look at the attached sample, the ViewModel constructor gets the data. If you bind the ViewModel to the XAML you can display sample data for design mode and access your service when not in design mode.

Here is a sample of a shared property that returns if the application is in design mode (running in the Blend or Visual Studio Designer):

Public Shared Property InDesignMode() As Boolean
        Get
            If Application.Current Is Nothing Or
               
 Application.Current.RootVisual Is Nothing Then
                Return True
            Else
                Return DesignerProperties.GetIsInDesignMode(
                                                       Application.Current.RootVisual)
            End If
        End Get 
        Private Set(ByVal value As Boolean
        End Set
    End Property

And here is the code for instantiating the ViewModel, Binding it to the DataContext of the layout and binding the collection in the ViewModel to the XamGrid:

<UserControl.Resources>
        <local:MainViewModel x:Key="ViewModel"/>
</UserControl.Resources>
    
<Grid x:Name="LayoutRoot" Background="White" 
    DataContext
="{Binding Source={StaticResource ViewModel}}">

        <igGrid:XamGrid
            Name="dgTimeSheet"
            ItemsSource="{Binding TaskCollection}">

...

Curtis Taylor

INFRAGISTICS
Killer Apps. No Excuses

  • Post Points: 5
Replied On: Wed, Nov 17 2010 3:29 PM Reply

2. If you notice in the first sample I posted that the data is not being updated in Project.Name. This is because I did not have all classes implement INotifyPropertyChanged. Attached is the same sample updated to support TwoWay binding. I added a ListBox at the bottom of the app to demonstrate that the underlying data is now updated when changing properties in the XamGrid.

Incidentally, when it comes to binding to an item in the ComboBox, I've found it to be extremely simple and less problematic to utilize an number to serve as an index into the ComboBox. The data source would be a number. You could use a converter to convert a string to the appropriate number.

3. If you use an ObservableCollection (as the sample data does in the attached sample), then if you need to dynamically update the data from the service at run-time, then the records you add or remove will be updated on the UI.

More in the next post...

Curtis Taylor

INFRAGISTICS
Killer Apps. No Excuses

  • Post Points: 5
Replied On: Wed, Nov 17 2010 4:35 PM Reply

To complete the set of questions:

4. I've updated the sample one last time to support this question. the best way to update the Total hours is not in the View but in the ViewModel or the Model. The sample data source is notified anytime a value is changed. So it is very easy to update the Total Hours property by simply summing the numbers and assigning them to that property. The sample will do just that.

5. And in like manner, since the sample is "date-driven", when a row is added, the ObservableCollection that is bound to the Grid is automatically updated with the new record. You no longer need to look at any index in the XamGrid itself. A best practice technique is to not deal with record management in the UI but in the data. Silverlight is designed to support the UI presenting and visualizing the data while the engine controls data logic back in the Model or ViewModel (which this sample illustrates).

Thank you!

Curtis Taylor

INFRAGISTICS
Killer Apps. No Excuses

  • Post Points: 5
Page 1 of 1 (4 items) | RSS