I cannot get the CurrentCellValidating to fire in GridgroupingControl

I have GridgroupingControl with 2 columns (code, Name) bind to a binding source, when I add a new row using the bindingsource. If I save the empty new record I cannot get the CurrentCellValidating to fire.

But if I enter values manually in the new row CurrentCellValidating works perfectly.

Can you help me?

//Retrieved the currencylist

var x = DataModule.SharedInstance.CurrencyList;

//Binds the DataSet to the binding source.

bindingSourceCurrencies.DataSource = x;

//pass the binding source to the GridgoupingControl

this.gridGCcurrencies.DataSource = bindingSourceCurrencies;

 

//add a new line to the BS which is reflect on GridgroupingControl

private void toolStripButtonAdd_Click(object sender, EventArgs e)

{

    bindingSourceCurrencies.AddNew();

    bindingSourceCurrencies.ResetCurrentItem();

    toolStripButtonAdd.Enabled = false;

}

//validate the first column

void gridGCcurrencies_CurrentCellValidating(object sender, GridTableControlCancelEventArgs e)

        { 

            GridCurrentCell currentCell = this.gridGCcurrencies.TableControl.CurrentCell;

            GridTableCellStyleInfo tableCellStyle = e.TableControl.GetTableViewStyleInfo(currentCell.RowIndex, currentCell.ColIndex);   

            if (tableCellStyle.TableCellIdentity.Column != null && tableCellStyle.TableCellIdentity.Column.Name == "Code")

            {

                if (string.IsNullOrEmpty(currentCell.Renderer.ControlText.Trim()))

               {

                   currentCell.SetError("Please cannot be null");

               }            

          

            }                    

        }

//Saving the changes to the backend

private void toolStripButtonSave_Click(object sender, EventArgs e)

 {

      JDSdb1.DataModule.SharedInstance.ApplyChanges();

      toolStripButtonAdd.Enabled = true;

 }


11 Replies

AR Arulpriya Ramalingam Syncfusion Team April 30, 2018 12:56 PM UTC

Hi Jocelyn, 
 
Thanks for using Syncfusion products. 
 
As per the GridGroupingControl current support, the CurrentCellValidating event will be triggered whenever, the cell loses focus. Whereas, while the records are added directly to binding source, the cell will not be focused. So that, the cell will not be validated. In order to validate the cell while adding the data directly to the binding source, the current cell can be moved to newly added row and the cell can validated by using the Validate() method in SourceListListChangedCompleted event. We have created a simple sample as per your requirement and please make use of the below code and sample, 
 
Code example 
 
//Event triggering 
gridGroupingControl1.SourceListListChangedCompleted += GridGroupingControl1_SourceListListChangedCompleted; 
 
//Event customization 
private void GridGroupingControl1_SourceListListChangedCompleted(object sender, TableListChangedEventArgs e) 
{ 
    if (e.ListChangedType == ListChangedType.ItemAdded) 
    { 
        int index = e.Table.Records.Count - 1; 
        //To set the newly added record as current record  
        e.Table.CurrentRecord = e.Table.Records[index]; 
        //To set the column as current cell 
        e.Table.CurrentRecord.SetCurrent("Code"); 
        //To trigger the CurrentCellValidating event. 
        gridGroupingControl1.TableControl.CurrentCell.Validate(); 
    } 
} 
 
 
Regards, 
Arulpriya 



JO Jocelyn May 1, 2018 12:42 AM UTC

Bonjour
Trigger the CurrentCellValidating event using the Validate() method in SourceListListChangedCompleted event work partialy 
I do see the validation error on the gridGCcurrencies but if I click on another line I can bypass the validation.
If I enter a value in the cell and then delete that value the same CurrentCellValidating event is fire but this time I am not able to bypass the validation which is the behavior I am looking for.

any Idea why?

Merci


AR Arulpriya Ramalingam Syncfusion Team May 1, 2018 05:17 AM UTC

Hi Jocelyn, 
 
Thanks for the update. 
 
We are little bit unclear of your requirement and we suspect that you are trying to restrict moving of current cell is the cell is not valid. If yes, the current cell can be maintained in the same cell for invalid values by handling the TableControlCurrentCellValidating event. Please make use of the below code, 
 
Code example 
 
//Event triggering 
gridGroupingControl1.TableControlCurrentCellValidating += GridGroupingControl1_TableControlCurrentCellValidating; 
 
//Event customization 
private void GridGroupingControl1_TableControlCurrentCellValidating(object sender, GridTableControlCancelEventArgs e) 
{ 
    GridCurrentCell currentCell = this.gridGroupingControl1.TableControl.CurrentCell; 
    GridTableCellStyleInfo tableCellStyle = e.TableControl.GetTableViewStyleInfo(currentCell.RowIndex, currentCell.ColIndex); 
    if (tableCellStyle.TableCellIdentity.Column != null && tableCellStyle.TableCellIdentity.Column.Name == "Code") 
    { 
        if (string.IsNullOrEmpty(currentCell.Renderer.ControlText.Trim())) 
        { 
            currentCell.SetError("Please cannot be null"); 
            // To restrict moving of current cell. 
            e.Inner.Cancel = true; 
        } 
    } 
} 
 
 
Note 
Since, we are unclear of the requirement, please let us know your exact scenario with simple screenshot/video. So, that we could provide you the solution at the earliest. 
 
Regards, 
Arulpriya 



JO Jocelyn May 1, 2018 11:43 PM UTC

Bonjour 
yes are trying to restrict moving of current cell if the cell is not valid.

When using (e.Inner.Cancel = true) the cell received the focus with the error which is perfect, if I try to move to another cell on the same row the grid block me which is also perfect.

But if I select a cell in another row I can bypass the validation.

see pdf

Merci.


SN Sindhu Nagarajan Syncfusion Team May 2, 2018 12:09 PM UTC

Hi Jocelyn, 
 
Thanks for the update. 
 
We have checked your reported scenario and able to reproduce the reported issue in below use case. 
  • Add new row with cell validation.
  • Move to another cell on different row.
  • Now CurrentCellValidating event is not invoked.
 
Video Link: Video 
 
As per current support, CurrentCellValidating event will be triggered when current cell value has been modified/edited. So, If you want to trigger validation whenever current cell loses the focus, set CurrentCell.IsModified to True. 
 
Code Example 
private void GridGroupingControl1_SourceListListChangedCompleted(object sender, TableListChangedEventArgs e) 
{ 
    if (e.ListChangedType == ListChangedType.ItemAdded) 
    { 
        int index = e.Table.Records.Count - 1; 
        e.Table.CurrentRecord = e.Table.Records[index]; 
        e.Table.CurrentRecord.SetCurrent("Code"); 
        gridGroupingControl1.TableControl.CurrentCell.IsModified = true;.                 
        gridGroupingControl1.TableControl.CurrentCell.Validate(); 
    } 
} 
 
If we misunderstood anything from your scenario, please provide us with simple video of your scenario execution. It would be helpful for us to provide the solution at the earliest. 
  
 
Regards, 
Sindhu  



JO Jocelyn May 3, 2018 12:57 AM UTC

Thank you work like a charm


SN Sindhu Nagarajan Syncfusion Team May 3, 2018 04:15 AM UTC

Hi Jocelyn, 

Thanks for the update.  
We are glad to hear that the provided solution resolved your scenario. Please let us know if you have any other queries. 

Regards, 
Sindhu


JO Jocelyn May 4, 2018 02:14 AM UTC

Bonjour one more thing

For the newly added row I have many columns (Code, Name, Status, ConversionRate, Date)

The validation works fine for the first column (Code), but when I move the next column (Name) how do I get the validation event to fire.

Same thing for every column on that new row they all have validations

Merci.




SN Sindhu Nagarajan Syncfusion Team May 4, 2018 12:37 PM UTC

Hi Jocelyn, 

Thanks for the update. 

If you want to validate all values in a newly added record, invoke CurrentCell.Validate() method for each cell of that row in the SourceListListChangedCompleted event. Please refer to the below code, 

Code Example 
private void GridGroupingControl1_SourceListListChangedCompleted(object sender, TableListChangedEventArgs e) 
{ 
    if (e.ListChangedType == ListChangedType.ItemAdded) 
    { 
        int index = e.Table.Records.Count - 1; 
        e.Table.CurrentRecord = e.Table.Records[index]; 
            foreach (GridColumnDescriptor column in this.gridGroupingControl1.TableDescriptor.Columns) 
        { 
            e.Table.CurrentRecord.SetCurrent(column.ToString()); 
            gridGroupingControl1.TableControl.CurrentCell.IsModified = true; 
            gridGroupingControl1.TableControl.CurrentCell.Validate(); 
        } 
    } 
} 
 

Please let us know if you have any other queries. 

Regards, 
Sindhu  



JO Jocelyn May 5, 2018 12:18 AM UTC

Bonjour
This code did loop in each new row cell but only the first cell validation is activate, once I pass that validation and move to the second cell I can bypass the validation.
exactly the same behavior than the previous code you gave me

private void gridGCcurrencies_SourceListListChangedCompleted(object sender, TableListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemAdded)
            {
                int index = e.Table.Records.Count - 1;
                //To set the newly added record as current record  
                e.Table.CurrentRecord = e.Table.Records[index];
                foreach (GridColumnDescriptor column in this.gridGCcurrencies.TableDescriptor.Columns)
                {
                    e.Table.CurrentRecord.SetCurrent(column.ToString());
                    gridGCcurrencies.TableControl.CurrentCell.IsModified = true;
                    gridGCcurrencies.TableControl.CurrentCell.Validate();
                } 
                
            }          
        } 
Merci.


SN Sindhu Nagarajan Syncfusion Team May 7, 2018 10:29 AM UTC

Hi Jocelyn, 
 
Sorry for the inconvenience caused. 
 
To validate all the values of the newly added row, CurrentCell.Validate() method is invoked when the current  cell is moved in TableControlCurrentCellMoved event. In order to trigger the validation, CurrentCell.IsModified  property is set to True. Please make use of the below code and sample, 
 
Code Example 
gridGroupingControl1.TableControlCurrentCellMoved += GridGroupingControl1_TableControlCurrentCellMoved; 
 
private void GridGroupingControl1_TableControlCurrentCellMoved(object sender, GridTableControlCurrentCellMovedEventArgs e) 
 { 
     if (isAdded && e.TableControl.CurrentCell.RowIndex == gridGroupingControl1.TableModel.RowCount ) 
     { 
         e.TableControl.CurrentCell.IsModified = true; 
         e.TableControl.CurrentCell.Validate(); 
     } 
 } 
 
 
 
Please let us know if you have any other queries. 
 
Regards, 
Sindhu  


Loader.
Up arrow icon