Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
660
Date + Time Input-Mask vor Column - How to ignore empty Time?
posted

Hi,

i like the user to add a Datei and - optionally - a time into a cell. 

So i set the Input-Mask like: this.dfgrid1.DisplayLayout.Bands[0].Columns["Datum"].MaskInput = "{date} {time}";

This works in a way, that the user can type along the date and the time.

BUT: If user only enters the date-part and leaves the time-part empty, an error occurs. Fair enough, but not what i like to happen :)

I would like to set the Time-Part to 00:00 if it isn't entered by the user. How could i do that? (I'm sure it's pretty simple but i'm looking at the wrong properties/events)

Parents
  • 469350
    Verified Answer
    Offline posted

    Hi,

    This is actually not very easy to do in a reliable way.

    One very simple way to do it would be like this:


           
            private void ultraGrid1_BeforeExitEditMode(object sender, Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs e)
            {
                var grid = (UltraGrid)sender;
                UltraGridCell activeCell = grid.ActiveCell;

                if (activeCell.Column.Key == "DateTime 1")
                {
                    string cellText = activeCell.Text;
                    if (cellText.EndsWith("__:__ __"))
                    {                    
                        cellText = cellText.Substring(0, cellText.Length - "__:__ __".Length);
                        DateTime newDateTime;
                        bool success = DateTime.TryParse(cellText, out newDateTime);
                        if (success)
                            activeCell.EditorResolved.Value = newDateTime;
                    }
                }
            }

    So this code explicitly looks to see if the Time portion of the DateTime is empty and if so, parses a date based solely on the Date portion of the text.

    But this code is flawed because it doesn't account for culture. I've hard-coded it to work with the culture on my machine. On my machine, "{date} {time}" returns a date with a 4-digit year and a time format with an colon and an AM/PM symbol. But this might vary on other machine which are set to different cultures. So the right thing to do would be to use the current culture to get the property strings and string lengths. Unfortunately, this is not simple and the DateTime editor doesn't use the system settings directly when you use "{time}" - it modifies the format slightly to provide 2 digits for the hour in the mask.

    So the code I have here will work in a simple case as long as the culture is the same on all machines running you application. Otherwise, I don't see any way to achieve what you want.

Reply Children