I have an Xceed DataGridControl on a WPF Window with a few columns that are data bound to a DataTable. The UpdateSourceTrigger
on the grid control is set to CellContentChanged
so that the binding source updates as soon as a cell's content changes.
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="375" Width="494" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid>
<xcdg:DataGridControl Margin="22,21,25,27" Name="MyTable" UpdateSourceTrigger="CellContentChanged" CellEditorDisplayConditions="MouseOverCell, CellIsCurrent">
<xcdg:DataGridControl.View>
<xcdg:TableView />
</xcdg:DataGridControl.View>
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="ProductID" Title="ProductID" IsMainColumn="True" />
<xcdg:Column FieldName="Description" Title="Description" />
<xcdg:Column FieldName="IsObsolete" Title="IsObsolete" />
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
</Window>
I added some code to handle the DataTable ColumnChanging
event. In the event handler, I added some validation to prevent the "IsObsolete" column's value from ever being set to True.
If I simply change the proposed value to False, the data source change is effectively rejected; however, the visual display of the checkbox in the grid remains checked until the user moves away from the row. (By the way, calling e.Row.RejectChanges()
surprisingly does not reject the change at all.)
The effect I want to achieve is for the checkbox never to appear checked if the validation fails.
This is what I've got so far:
Imports System.Data
Class Window1
Private _dt As System.Data.DataTable
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
_dt = New System.Data.DataTable("MyTable")
MyTable.ItemsSource = _dt.DefaultView
PopulateDataTable()
AddHandler _dt.ColumnChanging, AddressOf HandleStatusChanges
End Sub
Private Sub HandleStatusChanges(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
If String.Compare(e.Column.ColumnName, "IsObsolete", True) = 0 _
AndAlso CBool(e.ProposedValue) Then
e.ProposedValue = False
End If
End Sub
Private Sub PopulateDataTable()
_dt.Columns.Add(New System.Data.DataColumn("ProductID", GetType(String)))
_dt.Columns.Add(New System.Data.DataColumn("Description", GetType(String)))
_dt.Columns.Add(New System.Data.DataColumn("IsObsolete", GetType(Boolean)))
Dim dr As System.Data.DataRow
For row = 1 To 5
dr = _dt.NewRow
dr("ProductID") = "SB-" & row
dr("Description") = "Longboard"
dr("IsObsolete") = False
_dt.Rows.Add(dr)
Next
_dt.AcceptChanges()
End Sub
End Class
Thanks in advance!