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
1965
WinGrid - Validating the new row before adding it to the grid using new row template
posted

Hi,

I have a win grid with the new row template at the top and combo box outside the panel of the grid. When i click on the new row template, start editing it and leave before filling all the fields (i.e. i click on some other field outside grid panel), the new row gets  added to the grid which is undesirable as few of the mandatory fields are left blank. The new row needs to be validated before getting added to the grid.

I added this event BeforeRowUpdate and doing the following to validate the fields.

Though i am able to validate this way, but the partially filled new row is getting lost after this instead of being retained.

void gridOwner_BeforeRowUpdate(object sender, CancelableRowEventArgse)

{

if (e.Row.IsAddRow)

{

foreach ( UltraGridCell item in e.Row.Cells)

{

if (item.Value.ToString().Trim().Length <= 0 && !item.Column.Hidden)

{

MessageBox.Show("Please select a value for " + item.Column.Header.Caption.ToString());

e.Cancel = true;

break;

}

}

}

}

 

There fore, i am looking for some way to validate the new row before adding to the grid and even if the validation fails, the new row should not be discarded rather it should be held in the template where it is entered.

 

Thanks in advance,

Parthiban Sekar

Parents
No Data
Reply
  • 48586
    posted

    Hello ,

     

    I could suggest you to use BeforeRowDeactivate event and to put your validation code there. So in this event you should have code like:

     

                if (ultraGrid1.ActiveRow.IsAddRow)

                {

                    foreach (UltraGridCell item in ultraGrid1.ActiveRow.Cells)

                    {

                        if (item.Value.ToString().Trim().Length <= 0 && !item.Column.Hidden)

                        {

                            MessageBox.Show("Please select a value for " + item.Column.Header.Caption.ToString());

                            e.Cancel = true;

                            ultraGrid1.ActiveCell = item;

                            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);

                            break;

                        }

     

                    }

                }

     

     

    Also if you want to prevent user to commit AddRow pressing Enter (if the validation was not passed), then you should add the following code in load event of the form in order to modify default behavior of UltraGrid for pressing Enter key when the active row is AddRow

    foreach (GridKeyActionMapping key in ultraGrid1.KeyActionMappings)

                {

                    if (key.KeyCode == Keys.Enter && key.ActionCode == UltraGridAction.CommitRow && key.StateRequired == (UltraGridState.AddRow| UltraGridState.RowDirty))

                    {

                        key.StateRequired = UltraGridState.RowDirty;

                        key.StateDisallowed = UltraGridState.AddRow;

                    }

                }

     

    Please let me know if you have any further questions.

Children