Save / manage column layouts / sortings in WPF Data Grid

September 25, 2009

I present here a solution demonstrating Advanced Data Grid: a user control allowing you to save / retrieve / manage column layouts and sortings you have done in WPF Data Grid from WPFToolkit (see at wpf.codeplex.com). Due to security rules of wordpress.com, it cannot be uploaded as zip file, please firstly remove the .doc extension of file  Advanced Data Grid for WPF. Solution is developed under VS 2008.

It is not Open Source, you are not allowed to use the source code.
Description of code see in: CodeBeispiel (in German!), using manual here: developer manual for Advanced Data Grid (in English).

Advertisements

LINQ query: NullReferenceException

September 23, 2009

Code example:

1    Dim queryResult = customers.Where(Function (cu) cu.Type.id=myId)
2    If queryResult.Count>0 Then
3        End Sub
4    End If
5    …something

The line 1 runs OK, the lne 2 calls NullReferenceException!
You can think, because queryResult is empty? False!
queryResult was evaluated only at line 2, as the results are used.
It is “Deferred Evaluation” of LINQ.
And during the evaluation a Null Reference was found. For example, customer.Type is Nothing!

Think of it: the LINQ query will be evaluated, if it will be used, not before!

If you use the results in function .ToList():

1    Dim queryResult as List(Of customer) = customers.Where(Function (cu) cu.Type.id=myId).ToList()

then NullReferenceException will be called immediately in line 1, because the LINQ will be evaluated in the same code line, but only for function .ToList(), not in the LINQ expression itself!

Show custom Date format in auto-generated DataGrid column

June 25, 2009

The problem in WPF DataGrid is: data is always shown in US long format. Probably due to some bug in WPFToolkit. But we don’t want to change WPFToolkit. We try to avoid the issue.

Really useful link is:
http://stackoverflow.com/questions/848702/need-to-format-dates-in-dynamically-built-wpf-datagrid

The solution of Bryan Anderson:

<wpfToolkit:DataGrid>
   
<wpfToolkit:DataGrid.Resources>
       
<DataTemplate DataType="{x:Type DateTime}">
               
<TextBlock Text="{Binding StringFormat={0:d}}"  />
       
</DataTemplate>
   
</wpfToolkit:DataGrid.Resources>
    ...
</wpfToolkit:DataGrid> 

Doesn’t work by me ;-( ,

but the solution of FarrEver with AutoGeneratingColumn() event is a good idea.

But: it is still the US long! WPFToolikt doesn’t use computer’s short date format!

What we can do? We can hard code the format, but use the computer’s regional settings. Then your customers see the date format they used to see. Code:

Private Sub DGrid_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    If e.PropertyType Is GetType(DateTime) Then
        Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        If dataGridTextColumn IsNot Nothing Then
            Dim ShortDatePattern As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern
            dataGridTextColumn.Binding.StringFormat = “{0:” + ShortDatePattern + “}”

        End If
    End If
End Sub

Enforce DataGrid to regenerate columns

June 17, 2009

It is simple:

DataGrid.AutoGenerateColumns =False
DataGrid.AutoGenerateColumns =True
 
And columns will be regenerated!

Cancel Selection Change

May 20, 2009

The situation: the user has changed a selection in, say, combobox. Your event handler  Combobox_SelectionChanged() asks the user: “Are you sure?” and the user answers: “No! Cancel!” How can you cancel the selection?

One way is described in: http://amazedsaint.blogspot.com/2008/06/wpf-combo-box-cancelling-selection.html

Really previous selection is e.RemovedItems(0). But if in event handler  Combobox_SelectionChanged()you assign:

Combobox.SelectedItem = e.RemovedItems(0)

then your event handler will be called again! And again.

Amazedsaint proposed to use global variable. Not very clean solution. I propose to remove event handler assignment before the command Combobox.SelectedItem = e.RemovedItems(0) and reassign it afterwards, e.g.:

Sub Combobox_SelectionChanged()

If MessageBox.Show(“Are you sure”,MessageBox.YesNo)<>MessageBox.Yes Then

Dim cbo as ComboBox=CType(sender,ComboBox)

RemoveHandler Combobox.SelectionChanged, AddressOf Combobox_SelectionChanged

cbo.SelectedItem=e.RemovedItems(0)

AddHandler combobox.SelectionChanged, AddressOf combobox_SelectionChanged

End If

End Sub

One another issue with “Cancellation of User Changes” is changes made in binded data. If you don’t want to change data before the user assume it, you can use

UpdateSourceTrigger=Explicit

and change your data directly or via BindingExpression.UpdateSource().

Hope it helps.