Hi,
Just wondering if how to change the spinner increments for a DateTime column in a WinGrid? At the moment it defaults to a single minute - I'd like it to increment 15 minutes at a time - and not just cycle the minutes but increment the hours as required. Is there a way I can capture the spinner button press and manually alter the value?
Thanks in advance,
Scott
JMD_Rocks said:Can you please explain more what you mean by “you could change GetSpinIncrementResolved to return an object and then return some custom object that indicates that the Spin method needs to adjust AM/PM”. I did not get what exactly you are suggesting here.
So the way my sample works is that the Spin method calls into the GetSpinIncrementResolved. This method currently returns a DateTimeIncrement based on the CurrentSection of the DateTimeEditor. It then uses that DateTimeIncrement to adjust the date in the cell.
What I was suggesting is that you could change this method to return an object instead of a DateTimeIncrement. Then the Spin method would just check examine the returned object and if it's a DateTimencrement, just do what it's doing now, and if not, it would toggle the AM/PM. It occurs to me now that there is a much simpler way to handle it. You could just watch for an AMPMSection and use a DateTimeIncrement of 12 hours.
else if (currentSection is AMPMSection) dateTimeIncrement.Hours += 12;
JMD_Rocks said:
Also in code you mentioned below in GetSpinIncrementResolved method, can you please explain what full rights and permissions you are referring here?
// The Current Section is not exposed publicly, so we need to get it via reflection.
// This will, of course, only work if we have full rights and permissions.
For information about using reflection in C# and the required permissions, I recommend checking Microsoft's documentation. Basically, this should work fine in a Windows Forms application, but if you are running the app through a web browser or on a Network drive or anywhere the access rights are limited, this code will not work.
https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/security-considerations-for-reflection
Thank you for quick reply and sorry for my delay in response, got pulled in other critical task.
I have reviewed new sample and it seem this is what I am looking for… See details below:
The datatype of both column is DateTime and I am setting MaskInput = "hh:mm" to just show time field.
There is no problem if user end up accidentally spinning to a new Date as I am always using time portion and at the end I am converting time to start minutes and end minutes like below
For e.g time range start time ‘02:00AM and end time 3:00AM’ I am converting ‘Start Min: 120 End Min: 180’ and that is what I am saving in DB so I think date change is not an issue here.
Regarding AM/PM, yes I need to spin AM/PM as well, we have time format configurable so it is depend on that, if it set for 12 hours format then I do care for AM/PM and want to spin AM/PM as well.
Can you please explain more what you mean by “you could change GetSpinIncrementResolved to return an object and then return some custom object that indicates that the Spin method needs to adjust AM/PM”. I did not get what exactly you are suggesting here.
The mouse events of the EditorComponent don't fire because you are not mousing down on the EditorComponent, you are mousing down on the grid.
What's the DataType of the column here? If it's DateTime, then you might have to deal with the Date portion of the value. The user might end up accidentally spinning to a new Date, even if they are all the same initially. So that's something to watch out for.
In theory, you could use the MouseDown event of the grid. I'm not entirely sure that would work. There are a couple of problems. First, if the cell was not already in edit mode, the UIElements you need might not be there until the cell goes into edit mode on the first click. I suspect that's not an issue, but it's something to be aware of. More importantly, I don't think using MouseDown is a good solution, anyway. Because what if the user mouses down on the spin buttons? Or on Hour section, but then they use the arrow keys to move the caret into the minutes section?Unfortunately, since the editor doesn't expose a SelectionChange eventor a public way to get the "current" section, this will be very complicated and difficult to acheive. I got it working, for the most part, but I had to use reflection to get the current section and also handle the KeyDown event of the grid to trap for the up and down arrows keys. Here's an updated sample. This sample works for both the spin buttons in the cell and also for the up/down arrows. Clicking on the cell selects the entire text, though, and since there's no CurrentSection in that case, you will probably want to update the GetSpinIncrementResolved method and decide what to do there. Also, this code is not working for the AM/PM section. I don't know if that matters to you. Your screen shot doesn't have that, which leads me to believe that you might not be using DateTime - maybe you are using TimeSpan? Anyway, if that's something you need to handle, then maybe you could change GetSpinIncrementResolved to return an object and then return some custom object that indicates that the Spin method needs to adjust AM/PM. I didn't do any handling of the Date. So the Spin method should probably be updated such that after the call to increment.AdjustDateTime, you fix up the Date portion of the DateTime in case the user wraps around to a new day.
8156.WindowsFormsApp19.zip
Thanks for quick reply, I review your sample, this I am able to manage but I am looking for something like below:
I have two columns like “Start Time” and “End Time” like below
If my select hours section it should increment by an hour if I select minute section it should increment by 15 minutes. In your sample it by default increment with 15 minutes.
If I use UltraDateTimeEditor outside Grid and change Spin Increment on MouseDown Event of UltraDateTimeEditor like below then it work completely fine but when I set the this control in EditorComponent the mouse down event is not firing.
private void dateTimeEditor_MouseDown(object sender, MouseEventArgs e)
{
if (sender is UltraDateTimeEditor ultraDateTimeEditor)
var element = ultraDateTimeEditor.UIElement.ElementFromPoint(e.Location);
while (element != null)
if (element is SectionUIElement sectionElement)
if (sectionElement.Section is MinuteSection)
ultraDateTimeEditor.SpinIncrement = new DateTimeIncrement(0, 0, 0, 0, 5, 0, 0);
}
else if (sectionElement.Section is HourSection)
ultraDateTimeEditor.SpinIncrement = new DateTimeIncrement(0, 0, 0, 1, 0, 0, 0);
break;
element = element.Parent;
Can you help why Mouse down event is not fired when set with EditorComponent in grid?
Or is there a way this can be achieve?
Thank you in advance.
Here's a quick sample I whipped up.
WindowsFormsApp19.zip