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
Hi Scott,
The spinning is context sensitive. Do it depends on the location of the cursor. If the cursor is in the minute section, it spins a minute at a time. If it's in the hour section, it spins an hour at a time.
There's no way to change the incremement when using the built-in spinner. But you can do what you want using an editor. Put an UltraDataTimeEditor on the form with your grid. Don't set SpinButtonDisplayStyle. Use the ButtonsRight collection and add a SpinEditorButton.
Set the grid column's EditorContorl to the UltraDateTimeEditor.
Unlike the built-in spin button, this button does nothing by default. You have to code it using by handling the EditorSpinButtonClick event of the UltraDateTimeEditor. Use e.Context in this event to determine the Cell of the grid which is being spun.
Beautiful! Works like a charm.
Thanks for your help.
Here's a quick sample I whipped up.
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.
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
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.
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.
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:
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 Mike, appreciate all your help.
MinWidth, MaxWidth is working for me. this is what i was looking for. all set.
AutoSizeMode is not related to the size of the grid. That has to do with auto-sizing the width of the column to the contents of that column. The property you want here is grid.DisplayLayout.AutoFitSyle. In any case, what you can do is set MinWidth, MaxWidth, and Width to the same value on the columns you want to remain a fixed size - and then those columns will honor those settings when the grid AutoFit's the columns.
Hi Mike,
Thank you for all your help. I am all set for this issue.
Everything is working as expected now. We can close this thread marking as answered.
I have another question in general not related to this issue but for Ultragrid Dock = Fill.
I have UltraGrid placed in Panel with _grdDefinitions.Dock = DockStyle.Fill;
I have total 5 columns like below:
Start Time | End Time | Column 3 | Column 4 | Column 5
When i maximize my form all the columns are auto resized which is fine but i want Start Time and End Time columns to be fixed size and not resized automatically when form maximized, is there a way i can make this?
I tried grid Resize event and setting again width but did not work.
Also i tried colStart.AutoSizeMode = ColumnAutoSizeMode.None; but that also not worked.
I don't think -1 is a valid value for either SelectionStart or SelectionLength. You could try 0.
But I think there's a better way. Try setting CellClickAction on the column:
dateTimeColumn.CellClickAction = CellClickAction.Edit;
The default is CellClickAction.EditAndSelectText. So CellClickAction.Edit will place the caret wherever the user clicks. That only works when CLICKING on the cell, of course. If the user tabs into the cell, it still selects all of the text. So you could handle that like so:
private void UltraGrid1_AfterEnterEditMode(object sender, EventArgs e) { var grid = (UltraGrid)sender; var activeCell = grid.ActiveCell; var cellEditor = activeCell.EditorResolved; cellEditor.SelectionLength = 0; }
Notice that I am setting SelectionLength here, but NOT SelectionStart, because if I did that, you would lose the position of the caret when clicking on the cell.
I figured out AM/PM stuff same way you mentioned after I posted my reply.
This is working like a charm.
Thank you again for all your help, suggestions and quick replies.
Only one thing is when I click on Cell to Edit, by default it select all the text, just wanted to know is there a way I can unselect the default selected text and just keep caret at the place user clicked to edit?
I tried capturing UltraGrid1_AfterEnterEditMode event and setting below two properties to -1, and it unselect everything but it bring caret at 0th position up on edit mode, I was looking to keep caret where user click to edit, for e.g if user first click at minutes section to edit the cell then caret should be at minute section initially. Can you please see is there a way we can achieve this?
cellEditor.SelectionStart = -1;
cellEditor.SelectionLength = -1;