Blogs

The One With

Silverlight DataGrid - Creating custom column types for the grid control

     

image

 

AgDataGrid provides 5 build-in columns types.

1: AgDataGridTextColumn: Allows basic text editing.

2: AgDataGridDateColumn: Allows editing of the dates using a drop down calendar.

3: AgDataGridCheckColumn: Displays a check box and allows editing of bool types.

4: AgDataGridImageColumn: Displays an image inside a column.

5: AgDataGridColumn: Template column that allows custom display and inplace editor templates.

Let's see how to use it.

First, I will drop an AgDataGrid on my page and bind it to a list of Item objects.

<DevExpress:AgDataGrid x:Name="myGrid">
    <DevExpress:AgDataGrid.Columns>
        <DevExpress:AgDataGridTextColumn FieldName="Name"></DevExpress:AgDataGridTextColumn>
        <DevExpress:AgDataGridTextColumn FieldName="Points"></DevExpress:AgDataGridTextColumn>
     </DevExpress:AgDataGrid.Columns>           
</DevExpress:AgDataGrid>

public class Item {
    public string Name {
        get;
        set;
    }
    public int Points {
        get;
        set;
    }
}

myGrid.DataSource = new List<Item>() {
                new Item() { Name = "Item1", Points = 10 },
                new Item() { Name = "Item2", Points = 4 },
                new Item() { Name = "Item3", Points = 10 },
            };

 

image 

Instead of displaying Points as text, I want to display a little slider control. To do that, I will simply use the AgDataGridColumn.CellDisplayTemplate property

and assign a custom data template with a Slider control in it:

<DevExpress:AgDataGridColumn FieldName="Points">
                    <DevExpress:AgDataGridColumn.CellDisplayTemplate>
                        <DataTemplate>
                            <Slider Maximum="10" Minimum="0" LargeChange="1" IsHitTestVisible="False"/>
                        </DataTemplate>
                    </DevExpress:AgDataGridColumn.CellDisplayTemplate>

</DevExpress:AgDataGridColumn>

The end result is this:

image 

Exactly what I wanted!. One thing remains, binding it to a data value. We will use PrepareCellDisplayElement event to do that.

<DevExpress:AgDataGridColumn FieldName="Points" PrepareCellDisplayElement="AgDataGridColumn_PrepareCellDisplayElement">

private void AgDataGridColumn_PrepareCellDisplayElement(object sender,
    DevExpress.Windows.Controls.PrepareCellDisplayElementEventArgs e) {
    Slider slider = e.DisplayElement as Slider;
    slider.Value = (int)e.CellData.CellValue;
}

The DisplayElement that we get in the event args. corresponds to Slider control that we dropped in our data template. And CellData.CellValue refers to the value for the cell

that is about to be displayed.

 

There are two parts to developing custom column types. First part is to provide a template for displaying the value (which is what we did above) and the second part is to

provide a template for editing it. We will use the same slider control in our editing template.

<DevExpress:AgDataGridColumn.CellEditingTemplate>
                        <DataTemplate>
                            <Slider Maximum="10" Minimum="0"></Slider>
                        </DataTemplate>
</DevExpress:AgDataGridColumn.CellEditingTemplate>

We will need to hook into the inplace editing events of AgDataGridColumn to read and write the values from and to our slider control.

<DevExpress:AgDataGridColumn FieldName="Points"
                                             BeginEditing="AgDataGridColumn_BeginEditing"
                                             EndEditing="AgDataGridColumn_EndEditing">

 

private void AgDataGridColumn_BeginEditing(object sender, DevExpress.Windows.Controls.EditingEventArgs e) {
    (e.Editor as Slider).Value = (int)e.Value;
}

private void AgDataGridColumn_EndEditing(object sender, DevExpress.Windows.Controls.EditingEventArgs e) {
    e.Value = (int)(e.Editor as Slider).Value;
}

 

You can download this sample project from http://tv.devexpress.com/content/AgDataGrid/CustomColumnTypes/CustomColumnTypes.rar

 

Previous: Silverlight DataGrid - Binding an Image to a Row Preview within the grid control

Next: Silverlight DataGrid - Where are my Facebook friends?

 

Cheers,

Azret

Published Jun 20 2008, 07:19 PM by Azret Botash (DevExpress)
Filed under: ,
Technorati tags: Silverlight, AgDataGrid
Bookmark and Share

Comments

 

zac morris said:

AgDataGrid is looking nice and the video series is well done.

One question:  When binding to a custom column, why do we have to resort to handling events and marshaling data by hand instead of just using a binding like the following?

<DevExpress:AgDataGridColumn.CellEditingTemplate>

                       <DataTemplate>

                           <Slider Value="{Binding Points}"/>

                       </DataTemplate>

</DevExpress:AgDataGridColumn.CellEditingTemplate>

Is this kind of binding support planned for a future release?

Thanks.

June 25, 2008 2:10 PM
 

Evgeniy K (DevExpress) said:

With the next release, it will be possible to save editor values with the help of Binding. For now you can use it only for setting editor values.

June 26, 2008 9:06 AM
 

The One With said:

So I was writing a blog on how to bind data to AgDataGrid from a WebService call and got bored. AgDataGrid

July 5, 2008 6:24 PM
 

Community Blogs said:

Azret Botash with a series on the Devexpress Grid, Martin Mihaylov on JSON Serialization, Lee on UserControl

July 12, 2008 2:57 AM
 

Silverlight DataGrid - Binding an Image to a Row Preview within the grid control - The One With said:

Pingback from  Silverlight DataGrid - Binding an Image to a Row Preview within the grid control - The One With

July 15, 2008 1:30 PM
 

The One With said:

I am continuing to explore our Silverlight DataGrid and its features. In this video I talk about different

July 15, 2008 1:30 PM
 

cnblogs.com said:

ASPX: 1.在WEB项目中同时使用 C# AND VB 开发: 2. 微软SQL注入分析工具源代码下载。 3. 某些网站可能无法在 Internet Explorer 8 Beta 1 中正确显示的解决方案

July 17, 2008 10:29 AM
 

真见 said:

ASPX:

July 17, 2008 10:36 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.