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

Find if the user changed text using EditorRequired event handler

6 Answers 89 Views
GridView
This is a migrated thread and some comments may be shown as answers.
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
KARTHIK REDDY KAMIDI asked on 16 Feb 2011, 06:18 PM
Hello,

I have a unique requirement where I have to find out if a user manually entered a value in the cell or the system generated it. For example, in a cell it shows 10 as the current value and if the user typed in 10 again, the application has to know if 10 is typed in by user or not.

The cells could have any value type and so I am changing the editor in radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e). However, the handlers for the EditorElements are not being called when the user edits the cells. I am using RadComboBoxEditorElement, GridSpinEditorElement and RadTextBoxEditorElement.

private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
 {
     var varModel = radGridView1.CurrentRow.Tag as VariableModel;
 
     if (varModel.Variable != null)
     {
         var variableType = varModel.Variable.VariableType;
 
         if (varModel.Variable.VariableSelectionValueCollection.Count > 0)
         {
             var selValCol = varModel.Variable.VariableSelectionValueCollection;
             if (selValCol != null && selValCol.Count > 0)                          // if to be shown as a dropdown  // todo: vsv fix
             {
                 var editor = new RadComboBoxEditor();
                 var elem = editor.EditorElement as RadComboBoxEditorElement;
 
                 elem.DropDownWidth = radGridView1.Columns[STR_ValueCol].Width + radGridView1.Columns[STR_UnitsCol].Width;
                 elem.DropDownStyle = RadDropDownStyle.DropDownList;
                 elem.MaxDropDownItems = 10;
                 elem.DataSource = selValCol.Select(vs => vs.Description).ToList<string>();
 
                 editor.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
                 //editor.ValueChanged += new EventHandler(editor_ValueChanged);
 
                 e.Editor = editor;
             }
         }
         else if (variableType == VariableTypeEnum.Integer)
         {
             var editor = new GridSpinEditor();
             var elem = editor.EditorElement as GridSpinEditorElement;
             elem.MaxValue = int.MaxValue;
             elem.MinValue = int.MinValue;
             if (varModel.Variable.Val != null)
                 editor.Value = varModel.Variable.Val.ToInt32();
 
             e.Editor = editor;
         }
         else if (variableType == VariableTypeEnum.Double)                // todo : capture keys to exclude text
         {
             e.EditorType = typeof(RadTextBoxEditor);
             var editor = new RadTextBoxEditor();
             var elem = editor.EditorElement as RadTextBoxEditorElement;
             //elem.TextChanging += new TextChangingEventHandler(Editor_TextChanging);
             //elem.TextChanged += new EventHandler(elem_TextChanged);
             if (varModel.Variable.Val != null)
                 editor.Value = varModel.Variable.Val.ToDouble();
             e.Editor = editor;
         }
         else if (variableType == VariableTypeEnum.Boolean)
         {
             var editor = new RadComboBoxEditor();
             var elem = editor.EditorElement as RadComboBoxEditorElement;
             elem.Items.AddRange(new[] { new RadComboBoxItem("True"), new RadComboBoxItem("False") });
             editor.Value = elem.Items.Where(ri => ri.Text.ToLower() == varModel.Variable.Val.Val.ToLower()).FirstOrDefault();
 
             e.Editor = editor;
         }
         else if (variableType == VariableTypeEnum.String)
         {
             e.EditorType = typeof(RadTextBoxEditor);
             var editor = new RadTextBoxEditor();
             var elem = editor.EditorElement as RadTextBoxEditorElement;
             //elem.TextChanged += new EventHandler(elem_TextChanged);
             if (varModel.Variable.Val != null)
                 editor.Value = varModel.Variable.Val.ToString();
             e.Editor = editor;
         }
     }
 }


Thanks

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 16 Feb 2011, 08:05 PM
Hello,

If I understand your requirement correctly, then you could simply use the CellEndEdit() as my assumption is that the system updates will be done automatically.
private void radGridView1_CellEndEdit_1(object sender, GridViewCellEventArgs e)
{
    if (this.radGridView1.CurrentRow.Index > -1)
    {
      MessageBox.Show("UserX edited " + this.radGridView1.CurrentCell.Value.ToString());
     // can also get the databound item from this.radGridView1.CurrentRow.DataBoundItem
    }
}
You can also use the UserAddingRow event for added rows.

I hope you find this useful but let me know if you have any questions or if I have misunderstood your requirement
Richard
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 16 Feb 2011, 08:44 PM
Thank you, Richard, for your answer. The CellEndEdit will not work for me as this will be fired even if user presses Esc to get out of the edit mode. That's why I wanted to use the events of the EditorElements, but they are not working for me.

Thanks.
0
Richard Slade
Top achievements
Rank 2
answered on 16 Feb 2011, 08:56 PM
Hello,

In this case then it may be relevant to your situation to use a custom editor. In your custom editor for each type, you can log what you need. This uses a combination of the EditorRequired and CellEditorInitialized events. Please have a look at the documentation for custom editor at this link which I beleive will help you to achieve this.

If you need further help, please let me know
Thanks
Richard
0
Stefan
Telerik team
answered on 21 Feb 2011, 01:10 PM
Hi KARTHIK REDDY KAMIDI,

Thank you for writing.

I have another suggestion for you. You can check if a cell value is changed by creating a global variable of type GridViewCellInfo and subscribing to the CellBeginEdit and CellEndEdit events. Here is a sample of this:
GridViewCellInfo cell;
        void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            if (cell.Value != e.Value)
            {
                //here you know that the cell value is changed
            }
        }
        
        void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            //here you know that a cell is being clicked
            cell = radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

Could you please let me know which events do not fire for you and also which version of our suite are you using?

Greetings,
Stefan
the Telerik team
0
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
answered on 22 Feb 2011, 11:10 PM
Stefan,

Thank you for your suggestion. This still wouldn't help me. The scenario is as follows:

When the user starts editing, the system will show a calculated value in the cell, based on certain conditions. What I need to know is did the user type in the value even if it was already shown in the cell. ie did the user clear the cell and still type in the same value.

The reason is this: if the user typed in a value, even if it was already shown by the system, then the next time the system should not recalculate and show the user a different value, because the user specified it last time.

So I need to find if the user cleared it and typed in a value or if the user entered a value and then decided to cancel it by pressing Esc.

Thanks.
0
Stefan
Telerik team
answered on 25 Feb 2011, 08:39 AM
Hi KARTHIK REDDY KAMIDI,

Thank you for the clarification.

I have created a small application where I think I have covered your scenario. Please refer to the attached file. 

I hope this helps.
 
Kind regards,
Stefan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
KARTHIK REDDY KAMIDI
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or