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
530
Validate input in UltraGrid
posted

After reading a lot of comments here (like http://forums.infragistics.com/forums/p/36858/213294.aspx#213294) and making many tests I realize that validating data in an UltraGrid is not so easy, at least the way I like it:

1. keep focus in an invalid cell when the user leave that cell (for example by clicking a control capable to receive focus or not (inside the same form), even when that row is an AddRow or not. This is because some controls like a toolbar doesn't receive focus and I use that control to show a save data option, I mean, to commit new rows or edited rows to the data source - a DataSet in my case.

2. keep the value typed by the user so he/she can edit it.

I have spent days trying to get this working fine but the grid has so many members and there are a lot of suppositions around that it is so difficult to understand how it really works. I would like to have an example, clear, with good comments and no suppositions. Is it possible?

Parents
  • 469350
    Offline posted

    Hi,

    There isn't really one single all-encompassing way to do this. As you wrote, it's complicated. A lot depends on the needs of the application.

    But you can handle most cases uses the BeforeExitEditMode event. For example:


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

                string cellText = activeCell.Text;
                if (cellText == "A")
                {               
                    e.Cancel = true;
                }
            }

    Note that in a case like this, you must use the Text property of the cell. You cannot use Value, because this event fires before the Value is written to the cell. It has to be, otherwise you would get an error if the text was not valid for the underlying data type. 

    So this makes it a little tricky, since you will have attempt to convert the text into the proper data type for anything other than a string column.

    Also, this event only fires when the cell loses focus, so it won't work for clicking on a Toolbar or other control that does not take focus. But you can handle this by telling the grid to try to exit edit mode and then checking to see if the exit was successful before you do anything else.

    Here's some sample code. I used a Label)Click here since the label control does not take focus, so it's like a toolbar button in that respect.


            private void label1_Click(object sender, EventArgs e)
            {
                bool success = this.VerifyGridUpdated(this.ultraGrid1);
                if (success)
                    MessageBox.Show("The grid update was successful");           
            }

            private bool VerifyGridUpdated(UltraGrid grid)
            {
                // Check if the grid contains focus and has an active cell.
                if (grid.ContainsFocus && grid.ActiveCell != null)
                {
                    // Is the active cell in edit mode?
                    if (grid.ActiveCell.IsInEditMode)
                    {
                        // If we get here, it means that the grid has an active cell in
                        // edit mode. So we need to exit edit mode to commit the changes.
                        //
                        grid.PerformAction(UltraGridAction.ExitEditMode);

                        // If the cell is still in edit mode it means that the exit was
                        // probably cancelled. So return false to indicates ta the update was not
                        // successful.
                        if (grid.ActiveCell.IsInEditMode)
                            return false;
                    }
                }

                // If we get here, there was either no cell in edit mode or else
                // exiting edit mode succeeded. So commit the changes to the grid's underlying
                // data source.
                grid.UpdateData();

                // Return tru to indicate success.
                return true;
            }

     

Reply Children
No Data