This is a migrated thread and some comments may be shown as answers.

Cell Validation event in Grid and editors level

5 Answers 454 Views
GridView
This is a migrated thread and some comments may be shown as answers.
T S
Top achievements
Rank 1
T S asked on 28 Sep 2015, 02:30 PM

Hello,

Very new to Telerik Components.

 I am using Telerik Gridview with different column types like multicombobox, decimal, combo box etc., Now I need to do validation each cells and written code for validation in CellValidating event of Gridview. it is  validating  properly. but I need to clear the cell content which is not happening.

I red other articles regarding the same and i initialize a cell validating event in the editor (Multicombobox editor). in this case the event itself is not firing.

kindly help me sort this issue.

 I am attaching code snip shot for your ref.

Grid level Cell validation event

private void grddisplaygrid_CellValidating(object sender, CellValidatingEventArgs e)
        {
            e.Cancel = true;
            grddisplaygrid.CurrentRow.Cells[0].Value = string.Empty;
            }​

 

in editor level (keydown event is firing but the validaiting event is not firing

-----------------------

private void grddisplaygrid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
                if (this.grddisplaygrid.CurrentColumn is GridViewMultiComboBoxColumn)
                {
                    RadMultiColumnComboBoxElement editor = e.ActiveEditor as RadMultiColumnComboBoxElement;
                    if (editor != null)
                    {
                        iscellintialized = true;
                        editor.EditorControl.EnableFiltering = true;
                        editor.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        editor.ClearTextOnValidation = false;
                        editor.Text = "";
                        RadMultiColumnComboBoxElement editorElement = editor.EditorElement as RadMultiColumnComboBoxElement; //_gridEditor.EditorElement as RadItem;
                        editorElement.KeyDown += new KeyEventHandler(MultiComboboxeditor_KeyDown); //.KeyDown += new KeyEventHandler(radGridView1_KeyDown);
                        editorElement.Validating += new CancelEventHandler(editor_validaitng);
                        editorElement.Validated += new EventHandler(editor_validated);
                    }
                }
            }

        private void editor_validated(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void editor_validaitng(object sender, CancelEventArgs e)
        {
            //throw new NotImplementedException();
            
        }​

 

 

 

 

 

 

 

 

5 Answers, 1 is accepted

Sort by
0
T S
Top achievements
Rank 1
answered on 29 Sep 2015, 05:28 AM

Finally I got the solution to clear the cell value.

If it the cell type is ​Multicolumncombobox then

private void grddisplaygrid_CellValidating(object sender, CellValidatingEventArgs e)
        {

             RadMultiColumnComboBoxElement editor = e.ActiveEditor as RadMultiColumnComboBoxElement;
                if (editor != null)
                {
                    e.Cancel = true;
                    RadMultiColumnComboBoxElement editorElement = editor.EditorElement as RadMultiColumnComboBoxElement;
                    editorElement.Text = string.Empty;
                }
            } 

if column type is texteditor then

private void grdview_CellValidating(object sender, CellValidatingEventArgs e)
        {​

              e.Cancel = true; 
             e.ActiveEditor.BeginEdit();
             e.ActiveEditor.Value = string.Empty;
             e.ActiveEditor.EndEdit(); 

    }

Still one thing, I haven't got the solution that the validating event is not firing for cell type multicombobox editor element in grid . 

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Sep 2015, 12:42 PM
Hello ,

Thank you for writing.

I am glad that you have resolved the problem you were facing. Note that RadGridView provides a convenient way to perform validation before data is committed to the underlying data source. You can validate data by handling CellValidating event which is raised by RadGridView when the current cell changes or when the cell loses input focus (when pressing Enter key). Canceling this event prevents the user from exiting the cell until a valid editor value is entered or the process is canceled (via Esc key). In this the editor is still active. Hence, you should clear the editor's value, not the cell's value. 

As to the question about the Validating/Validated events, you should subscribe to the RadMultiColumnComboBoxElement.TextBoxElement.TextBoxItem's events instead.
RadMultiColumnComboBoxElement editorElement = editor.EditorElement as RadMultiColumnComboBoxElement; 
editorElement.TextBoxElement.TextBoxItem.Validating += new CancelEventHandler(editor_validating);
editorElement.TextBoxElement.TextBoxItem.Validated += new EventHandler(editor_validated);

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
T S
Top achievements
Rank 1
answered on 29 Sep 2015, 04:35 PM

Thank you very much for kind response and support. i will the implement the same and give a try.

I have one more issue pertaining to the Gridview.

I enabled adding of new row and contains.many columns also.

In add mode some columns will be disabled (read only) based on condition. but as part of editing of the added rows, I need to enable the columns which are disabled as part of add operations.

kindly help me to solve this issue. 

 

 

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Sep 2015, 01:17 PM
Hello,

Thank you for writing back.
 
You can use the ViewCellFormatting event and disable the desired cell when your current row is the new row:
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Row is GridViewNewRowInfo && e.Column.Name == "col1")
    {
        e.CellElement.Enabled = false;
    }
    else
    {
        e.CellElement.Enabled = true;
    }
}

Alternatively, you can manipulate the ReadOnly property of the column when the current row is changed. Thus, when you are at the new row, make the column read-only. Otherwise, it is editable:
private void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    if (e.CurrentRow is GridViewNewRowInfo)
    {
        this.radGridView1.Columns.Last().ReadOnly = true;
    }
    else
    {
        this.radGridView1.Columns.Last().ReadOnly = false;
    }
}

Feel free to use this approach which suits your requirement best.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
T S
Top achievements
Rank 1
answered on 02 Oct 2015, 05:10 AM

hello,

 Thank you very much for kind support. i implemented the logic and its working fine. 

Tags
GridView
Asked by
T S
Top achievements
Rank 1
Answers by
T S
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or