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.
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
Hi Mike,
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;
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.
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.
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.