GridColumnDataEventArgs Class
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v19.1.dll
Declaration
public class GridColumnDataEventArgs :
ColumnDataEventArgsBase
Public Class GridColumnDataEventArgs
Inherits ColumnDataEventArgsBase
To learn more, see Unbound Columns.
Example
This example demonstrates how to emulate row selection via an unbound column with a checkbox. The grid’s built-in editing is used. It’s enabled when the view’s NavigationStyle property is set to CellNavigation.
Starting from the 12.1.8 version, we support a new way of implementing multiple row selection via a checked column. Please refer to the following article, which shows how to implement this: How to implement multiple row selection behavior via a checked column.
<Window x:Class="UnboundCheckColumn.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="600" Width="400">
<StackPanel>
<Button Click="BtnInvert_Click">Invert selection</Button>
<Button Click="BtnGetSelected_Click">Get selection</Button>
<dxg:GridControl x:Name="grid" CustomUnboundColumnData="grid_CustomUnboundColumnData" Height="500" ItemsSource="{Binding List}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Id" />
<dxg:GridColumn FieldName="Number" />
<dxg:GridColumn FieldName="Selected" UnboundType="Boolean" AllowSorting="False">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:CheckEdit IsChecked="{Binding Data.Selected}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView x:Name="view" AutoWidth="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnboundCheckColumn {
public class SelectionHelper<KeyType> {
Dictionary<KeyType, bool> selectedValues = new Dictionary<KeyType, bool>();
public bool GetIsSelected(KeyType key) {
bool isSelected;
if(selectedValues.TryGetValue(key, out isSelected))
return isSelected;
return false;
}
public void SetIsSelected(KeyType key, bool value) {
if(value)
selectedValues[key] = value;
else
selectedValues.Remove(key);
}
public List<KeyType> GetSelectedKeys() {
List<KeyType> list = new List<KeyType>();
foreach(KeyType key in selectedValues.Keys)
list.Add(key);
return list;
}
public string GetSelectedKeysAsString() {
List<KeyType> list = GetSelectedKeys();
StringBuilder str = new StringBuilder();
for(int i = 0; i < list.Count; i++)
str.AppendLine(list[i].ToString());
return str.ToString();
}
public int GetSelectionCount() {
return selectedValues.Count;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnboundCheckColumn {
public class TestData {
public List<TestDataItem> List { get; private set;}
public TestData() {
List<TestDataItem> list = new List<TestDataItem>();
for(int i = 0; i < 20; i++) {
list.Add(new TestDataItem() { Id = Guid.NewGuid(), Number = i });
}
List = list;
}
}
public class TestDataItem {
public Guid Id { get; set; }
public int Number { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Grid;
namespace UnboundCheckColumn {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
TestData data = new TestData();
SelectionHelper<Guid> selectionHelper = new SelectionHelper<Guid>();
public Window1() {
InitializeComponent();
DataContext = data;
}
private void grid_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) {
if(e.Column.FieldName != "Selected") return;
Guid key = (Guid)e.GetListSourceFieldValue("Id");
if(e.IsGetData)
e.Value = selectionHelper.GetIsSelected(key);
if(e.IsSetData)
selectionHelper.SetIsSelected(key, (bool)e.Value);
}
private void BtnInvert_Click(object sender, RoutedEventArgs e) {
for(int i = 0; i < data.List.Count; i++) {
int rowHandle = grid.GetRowHandleByListIndex(i);
bool newIsSelected = !selectionHelper.GetIsSelected(data.List[i].Id);
grid.SetCellValue(rowHandle, "Selected", newIsSelected);
}
}
private void BtnGetSelected_Click(object sender, RoutedEventArgs e) {
string caption = string.Format("Selected rows (Total: {0})", selectionHelper.GetSelectionCount());
MessageBox.Show(selectionHelper.GetSelectedKeysAsString(), caption);
}
}
}
Imports System.Collections.Generic
Imports System.Text
Public Class SelectionHelper(Of KeyType)
Dim selectedValues As Dictionary(Of KeyType, Boolean) = New Dictionary(Of KeyType, Boolean)
Public Function GetIsSelected(ByVal key As KeyType) As Boolean
Dim isSelected As Boolean
If selectedValues.TryGetValue(key, isSelected) Then
Return isSelected
End If
Return False
End Function
Public Sub SetIsSelected(ByVal key As KeyType, ByVal value As Boolean)
If value Then
selectedValues(key) = value
Else
selectedValues.Remove(key)
End If
End Sub
Public Function GetSelectedKeys() As List(Of KeyType)
Dim list As New List(Of KeyType)()
For Each key As KeyType In selectedValues.Keys
list.Add(key)
Next key
Return list
End Function
Public Function GetSelectedKeysAsString() As String
Dim list As List(Of KeyType) = GetSelectedKeys()
Dim str As New StringBuilder()
For i As Integer = 0 To list.Count - 1
str.AppendLine(list(i).ToString())
Next i
Return str.ToString()
End Function
Public Function GetSelectionCount() As Integer
Return selectedValues.Count
End Function
End Class
<Application x:Class="UnboundCheckColumn.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace UnboundCheckColumn
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Public Class TestData
Public Property List As List(Of TestDataItem)
Public Sub New()
List = GetTestData()
End Sub
Function GetTestData() As List(Of TestDataItem)
Dim data As New List(Of TestDataItem)()
For index = 1 To 20
data.Add(New TestDataItem() With {.Id = Guid.NewGuid(), .Number = index})
Next
Return data
End Function
End Class
Public Class TestDataItem
Private privateId As Guid
Public Property Id() As Guid
Get
Return privateId
End Get
Set(ByVal value As Guid)
privateId = value
End Set
End Property
Private privateNumber As Integer
Public Property Number() As Integer
Get
Return privateNumber
End Get
Set(ByVal value As Integer)
privateNumber = value
End Set
End Property
End Class
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports DevExpress.Xpf.Grid
Namespace UnboundCheckColumn
Partial Public Class Window1
Inherits Window
Private data As TestData
Private selectionHelper As New SelectionHelper(Of Guid)()
Public Sub New()
InitializeComponent()
data = New TestData()
DataContext = data
End Sub
Private Sub grid_CustomUnboundColumnData(ByVal sender As Object, ByVal e As GridColumnDataEventArgs)
If e.Column.FieldName = "Selected" Then
Dim key As Guid = CType(e.GetListSourceFieldValue("Id"), Guid)
If e.IsGetData Then
e.Value = selectionHelper.GetIsSelected(key)
End If
If e.IsSetData Then
selectionHelper.SetIsSelected(key, CBool(e.Value))
End If
End If
End Sub
Private Sub BtnInvert_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
For i As Integer = 0 To data.List.Count - 1
Dim rowHandle As Integer = grid.GetRowHandleByListIndex(i)
Dim newIsSelected As Boolean = Not selectionHelper.GetIsSelected(data.List(i).Id)
grid.SetCellValue(rowHandle, "Selected", newIsSelected)
Next i
End Sub
Private Sub BtnGetSelected_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim caption As String = String.Format("Selected rows (Total: {0})", selectionHelper.GetSelectionCount())
MessageBox.Show(selectionHelper.GetSelectedKeysAsString(), caption)
End Sub
End Class
End Namespace
See Also