Blogs

The Progress Bar - DevExpress XPF Blog

Silverlight Grid Control – How to Embed Editors

     

The Silverlight Grid Control from DevExpress includes over 15 in-place editors that can be used to appropriately format the data as well as provide an interactive and user-friendly way of editing cell values. This is a feature that’s perhaps often overlooked by developers, but very useful for end-users who work with the data displayed in the grid every day. So to that effect, I’ve prepared this step-by-step tutorial on how to embed editors in grid cells.

Note that for the sake of simplicity, I’m using a list of items as my data source. This is in no way a requirement to using editors with data. Along the way, I’ll also show the code that can be used when working with a grid bound to data using RIA Services.

1. So to start, I’ll create a new Silverlight Application Project and add a grid control to it.

2. I’ll bind the grid to the sample list of data through code:

public MainPage() {
    InitializeComponent();
    grid.DataSource = new ProductList();
}

3. I’ll manually create the appropriate columns so it looks like this:

DevExpress_Silverlight_Grid_Columns

The XAML used to create this at design-time is as follows:

<dxg:GridControl x:Name="grid">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="ProductName">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings DisplayMember="ProductName"
                                        ValueMember="ProductName" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn FieldName="UnitPrice">
            <dxg:GridColumn.EditSettings>
                <dxe:SpinEditSettings MaxValue="999" MinValue="1" DisplayFormat="c2" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn FieldName="Quantity" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True" />
    </dxg:GridControl.View>
</dxg:GridControl>

4. Note that in the code above, the “ProductName” and “UnitPrice” columns have their column EditSettings property modified. The item inside those tags is the in-place editor. In the case of the ComboBoxEdit, I have specified the DisplayMember as well as the ValueMember properties so the editor knows which field to use to display the data and to populate the combo box.

The SpinEdit control simply has maximum value, a minimum value and the number’s display format specified.

5. If I run the application now, you’ll notice that the combo box is embedded inside the cell, but nothing is being displayed and when invoked, it has not been populated with data.

DevExpress_Silverlight_Grid_Editor_Empty

This is because while we have specified which fields to get data from, the editor control is unaware of which data source to query.

6. To fix that issue, I’ll need to also specify where the combo box should get its data from. For this, I’ll add the following line of C# code:

...
using DevExpress.Xpf.Editors.Settings;
...

public MainPage() {
    InitializeComponent();
    grid.DataSource = new ProductList();
    ((ComboBoxEditSettings)grid.Columns["ProductName"].EditSettings).ItemsSource = new ProductList();
}

7. As an example and a variation of the code above, if I had bound the grid control to a table named “Products” using RIA Services, the code would be as follows:

...
using System.ServiceModel.DomainServices.Client;
using DXProjectName.Web;
using DevExpress.Xpf.Editors.Settings;
...

private ProductsContext _productsContext = new ProductsContext();
public MainPage() {
    InitializeComponent();
    LoadOperation<Products> loadOperation = _productsContext.Load(_productsContext.GetProductsQuery());
    grid.DataSource = loadOperation.Entities;
    ((ComboBoxEditSettings)grid.Columns["ProductName"].EditSettings).ItemsSource = loadOperation.Entities;
}

8. And that’s it! When I run the application again, the end-result looks like this:

DevExpress_Silverlight_Grid_ComboBox_Edit

The project for this example can be downloaded from CodeCentral: http://www.devexpress.com/Support/Center/e/E2603.aspx

Published Dec 21 2010, 02:01 PM by Emil Mesropian (DevExpress)
Filed under: , , ,
Technorati tags: Editors, Silverlight, Grid, DXGrid
Bookmark and Share

Comments

 

The Progress Bar - DevExpress XPF Blog said:

I have covered data binding the Silverlight Grid Control using WCF RIA Services in the past, but have

January 28, 2011 6:47 PM
 

Lorenzo Viola said:

Hello

I'm in need to embed an HTML editor, but I see that it is not available in the list of embeddable editors.

There is some easy way to get HTML editing (example, RichTextBox) inside the silverlight gridcontrol ?

Best Regards

February 24, 2011 11:09 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.