We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridDataBoundControl on new row event

Dear All :

If we want to add code (for check current row data) before new row add.
Do  you have event for that 

Just like RowsDeleting event, we can do e.cancel 

Thanks

7 Replies

AR Arulpriya Ramalingam Syncfusion Team April 24, 2017 12:47 PM UTC

Hi Michael, 

Thank you for your interest in Syncfusion products. 

We have analyzed your scenario and we suspect that you are trying to Validate an entire row before adding. By default, in GridDataBoundGridControl the new value will be added to the Grid when the AddNewRow is in EditingMode and it does not have the direct support to validate the entire row.  

Note 
We would like to suggest GridGroupingControl for your requirement. It is used to display the binding source with more flexible manipulation like sorting, grouping, filtering. We have created a simple sample as per requirement using GridGroupingControl. Also GridGroupingControl does not have the direct support to validate an entire row , but this can be achieved by some work around. The CurrentRecordContextChange event can be used to validate an entire row. In that event , you can validate the column values of new record by getting the value using Record.GetValue() method. Please make use of below code and sample, 
 
Code snippet 
//Event Triggering 
this.gridGroupingControl1.CurrentRecordContextChange += GridGroupingControl1_CurrentRecordContextChange; 
//Event Customization 
private void GridGroupingControl1_CurrentRecordContextChange(object sender, CurrentRecordContextChangeEventArgs e) 
{ 
    int colIndex = this.gridGroupingControl1.TableControl.CurrentCell.ColIndex; 
    //To move the right cell when pressing Enter on AddNewRecordRow and restrict the new record being added when enter the value until last column 
    if (e.Action == Syncfusion.Grouping.CurrentRecordAction.EndEditCalled && e.Table.AddNewRecord.IsCurrent 
        && colIndex != this.gridGroupingControl1.TableControl.TableDescriptor.Columns.Count) 
    { 
        e.Cancel = true; 
        this.gridGroupingControl1.TableControl.CurrentCell.MoveTo(this.gridGroupingControl1.TableControl.CurrentCell.RowIndex, colIndex + 1, GridSetCurrentCellOptions.SetFocus); 
    } 
    if (e.Action == Syncfusion.Grouping.CurrentRecordAction.EndEditCalled && e.Table.AddNewRecord.IsCurrent 
        && colIndex == this.gridGroupingControl1.TableControl.TableDescriptor.Columns.Count) 
    { 
        //Validate the current record values and defines whether it can be added or not. 
        if ((int)e.Record.GetRecord().GetValue("CategoryID") > 100) 
        { 
            e.Cancel = true; 
        } 
    } 
} 
 
 

Sample link: http://www.syncfusion.com/downloads/support/forum/130099/ze/DataGrid_20151195234678  

 
Please let us know if we misunderstood anything. 

Regards, 
Arulpriya 



MK Michael K April 24, 2017 02:55 PM UTC

Dear Arulpriya :

Thanks for your coding and I will try on it.

Howvever, when I try to write a project to about Control GridDataBoundGrid, I find the below problems, Can you help ?
1. When I set EnableAddNew = false
   when I input the value at the last column and last row of GridDataBoundGrid and press enter or tab key, it will not trigger the event CurrentCellEditingComplete. For other column is work. 
2. I make a control to inherit GridDataBoundGrid, and try override the function CurrentCellEditingComplete, it can't. What I want it, I would like to write the common function on after column[x] value edit. How can do it ?

Thanks for your help

Michael


AR Arulpriya Ramalingam Syncfusion Team April 25, 2017 08:52 AM UTC

Hi Michael, 

Thanks for your update. 

By default, the CurrentCellEditingComplete event will be triggered when the CurrentCell is moved to next cell and the current cell is moved to right cell on Enter key pressed. If the Enter key is pressed on last column, there is no right cell to move, so the current cell will be remain in same. Your scenario can be achieved by some workaround using CurrentCellKeyDown event. In that event, the current cell is moved to 1st column of next row if the current cell is in last column by using GridCurrentCell.MoveTo() method. Please make use of below code and sample, 

Code snippet 
//Event triggering 
this.gridDataBoundGrid1.CurrentCellKeyDown += GridDataBoundGrid1_CurrentCellKeyDown1; 

//Event customization 
private void GridDataBoundGrid1_CurrentCellKeyDown1(object sender, KeyEventArgs e) 
{ 
    if(e.KeyCode==Keys.Enter) 
    { 
        GridCurrentCell currentCell = gridDataBoundGrid1.CurrentCell; 
        if (currentCell.ColIndex==this.gridDataBoundGrid1.GridBoundColumns.Count) 
        { 
            if (currentCell.RowIndex == this.gridDataBoundGrid1.Binder.RecordCount) 
                //To move the first cell if the current cell is last column of the last row 
                currentCell.MoveTo(1, 1); 
            else 
                //To move the first column of next row 
                currentCell.MoveTo(currentCell.RowIndex + 1, 1); 
            e.Handled = true; 
        } 
         
    } 
} 
//Event triggering 
this.gridDataBoundGrid1.CurrentCellEditingComplete += GridDataBoundGrid1_CurrentCellEditingComplete; 
 
//Event customization 
private void GridDataBoundGrid1_CurrentCellEditingComplete(object sender, EventArgs e) 
{ 
   //Code to perform  
} 


Regards, 
Arulpriya 



MK Michael K April 25, 2017 02:35 PM UTC

Dear Arulpriya :

Thanks for your advice, and your advice is usefull for solve question 1
for question 2
How can I handle on override function of CurrentCellEditingComplete  in GridDataBoundGrid.
It seem the control not allow me to override, but I need to write common function on after column edit.

Thank you

Michael


AR Arulpriya Ramalingam Syncfusion Team April 26, 2017 06:26 AM UTC

Hi Michael, 

Thanks for your update. 

The CurrentCellEditingComplete event can be overridden by OnCurrentCellEditingComplete method of the custom DataBoundGrid control which implements the GridDataBoundGrid class. This OnCurrentCellEditingComplete  method will be triggered when the current cell editing is completed. Please make use of below code and sample, 

Code snippet 
 
//Custom DataBoundGrid class 
public class CustomDataBoundGrid: GridDataBoundGrid 
{ 
    protected override void OnCurrentCellEditingComplete(EventArgs e) 
    { 
        base.OnCurrentCellEditingComplete(e); 
        /// 
        ///Code to perform 
        /// 
    } 
} 
//Event Triggering 
this.customGrid.CurrentCellEditingComplete += CustomGrid_CurrentCellEditingComplete; 
 
//Event customization 
private void CustomGrid_CurrentCellEditingComplete(object sender, EventArgs e) 
{ 
    //Code to perform 
} 


Regards, 
Arulpriya


MK Michael K April 28, 2017 03:25 AM UTC

Hi :

I get it , thanks a lot

:)

Cheer


AR Arulpriya Ramalingam Syncfusion Team April 28, 2017 03:59 AM UTC

Hi Michael,   

We are glad to hear that the provided solution is resolved your scenario.   

Please let us know if you need any further assistance.    
   
Regards,   
Arulpriya 


Loader.
Live Chat Icon For mobile
Up arrow icon