I've got a TimeSpan property that I want to edit in an UltraGridCell. I've got the EditorDataFilter code so that it works in a kind of "natural language" manner (e.g., 1d 3h 2m for 1 day, 3 hours, and 2 minutes). That part works great. What doesn't work is that I've specified a mask that requires the user to enter the text into the cell in that format, but the mask isn't doing anything at all. I have other scenarios with both Framework types and custom types that I need this kind of support for. What should I do?
Okay, that works. There's a problem with the code I gave you (it tries to parse "4h" into a number), but that's my error. Setting the expected type to System.String makes the editor happy. I was thinking that ExpectedType was the type of the value being edited (e.g., System.TimeSpan). If you're using a DataFilter to manipulate the displayed text, I'm not sure what ExpectedType buys you exactly, but it's no problem to set that before using it. Thanks for your help Mike.
Chris McKenzie
I've thought about this a little more, and I think there might be a potential solution. The problem here is that you are trying to use a TimeSpan as the editor value. As I explained, the EditorWithMask doesn't know what to do with this.
If you used a different data type for the column - like a string or a double or some other numeric type that the EditorWithMask does know how to do deal with, then it might work. The Owner value in this case would still be a TimeSpan. So the OwnerToEditor conversion would have to convert the TimeSpan into something that the EditorWithMask can handle. I think a String would probably be the easiest thing. The EditoToOwner conversion would then have to take the string and transform it into a TimeSpan object. I haven't tried this out, and it will probably require some trial-and-error, but it seems feasible.
Okay, I took a look at your sample code, and I don't see any way to get this to work. The EditorWithMask simply does not know what to do with a TimeSpan object. It doesn't have any way to deal with in terms of how to display it or break it up into masked sections. The EditorWithText can deal with it, because all it has to do is dislpay it. To do that, it just calls ToString on the object. But the EditorWithMask has to know what to do with the value and how to place each part into the proper place in the mask. Your mask here is using a couple of # signs. But even if this worked, the EditorWithMask has no way to get values for those numbers. When it looks at the Value of the cell, it's getting back a TimeSpan object, which is does not recognize. It can't know that the first number should be the Hours property and second should be Minutes.
Of course, the control could be updated to deal with TimeSpan's specifically, but it does not right now. So I recommend that you Submit a feature request to Infragistics and perhaps this functionality can be added in a future release.
"You just need to set the ExpectedType on the editor."
Yeah, I went down that road. I get "System.Exception: The Owner specified a type to EditorWithMask that is not supported by the editor."
I created a code sample to demonstrate something like what I'm trying to do. To set this up, you just need to add a form to a windows application project in visual studio 2008. Drag an UltraGrid onto the form and don't set anything up on it (i.e., just click "finish" when the designer dialog comes up). Everything else that happens is in this code sample.
I hope I'm just doing something horribly wrong.
Public Class Form2
Private Class MyCustomType
Private _Id As Long Public Property Id() As Long <System.Diagnostics.DebuggerStepThrough()> Get Return _Id End Get <System.Diagnostics.DebuggerStepThrough()> _ Set(ByVal value As Long) _Id = value End Set End Property
Private _Description As String Public Property Description() As String <System.Diagnostics.DebuggerStepThrough()> Get Return _Description End Get <System.Diagnostics.DebuggerStepThrough()> _ Set(ByVal value As String) _Description = value End Set End Property
Private _Duration As TimeSpan Public Property Duration() As TimeSpan <System.Diagnostics.DebuggerStepThrough()> Get Return _Duration End Get <System.Diagnostics.DebuggerStepThrough()> _ Set(ByVal value As TimeSpan) _Duration = value End Set End Property
End Class
Private Class DataFilter Implements Infragistics.Win.IEditorDataFilter
Public Function Convert(ByVal conversionArgs As Infragistics.Win.EditorDataFilterConvertArgs) As Object Implements Infragistics.Win.IEditorDataFilter.Convert Dim objResult As Object = Nothing
Select Case conversionArgs.Direction Case Infragistics.Win.ConversionDirection.OwnerToEditor Dim objValue As TimeSpan = CType(conversionArgs.Value, TimeSpan) objResult = String.Format("{0}h {1}m", objValue.Hours, objValue.Minutes) conversionArgs.Handled = True conversionArgs.IsValid = True
Case Infragistics.Win.ConversionDirection.EditorToOwner Dim strValue As String = conversionArgs.Value.ToString If System.Text.RegularExpressions.Regex.IsMatch(strValue, "^\d+h \d+m$") Then Dim args() As String = strValue.Split(" "c) Dim intHours As Integer = CInt(args(0)) Dim intMinutes As Integer = CInt(args(1)) Dim result As New TimeSpan(intHours, intMinutes, 0) objResult = result
conversionArgs.Handled = True conversionArgs.IsValid = True End If End Select
Return objResult End Function End Class
Private BindingSource As New BindingSource()
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.InitializeGrid()
Dim data As List(Of MyCustomType) = Me.GetData Me.BindingSource.DataSource = data End Sub
Private Sub InitializeGrid() ' ' this line should create the columns in the grid. ' Me.BindingSource.DataSource = New List(Of MyCustomType) Me.UltraGrid1.DataSource = Me.BindingSource
' ' Change the duration column to use an EditorWithMask ' Dim column As Infragistics.Win.UltraWinGrid.UltraGridColumn = _ Me.UltraGrid1.DisplayLayout.Bands(0).Columns("Duration")
Dim objEditor As New Infragistics.Win.EditorWithMask objEditor.ExpectedType = GetType(TimeSpan) objEditor.DataFilter = New DataFilter
column.Editor = objEditor column.MaskInput = "#h #m" End Sub
Private Function GetData() As List(Of MyCustomType) Dim results As New List(Of MyCustomType) For i As Integer = 0 To 4 Dim result As New MyCustomType result.Id = i result.Description = "Test: " + i.ToString("000") result.Duration = New TimeSpan(i, i, i, i) results.Add(result) Next
Return results End Function
Hi Chris,
crmckenzie said:I tried using the EditorWithMask, but it just completely blew up. Apparently it's coded only to recognize specific types, and woe unto you if you need to control the formatting of a custom type.
You just need to set the ExpectedType on the editor.