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

Customize editor of GridViewMultiComboBoxColumn throw cast exception

4 Answers 126 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Le
Top achievements
Rank 1
Le asked on 07 Jan 2014, 04:31 AM
Hi Telerik Team 

I customize to inherit GridViewMultiComboBoxColumn, BaseGridBehavior and RadMultiColumnComboBox. Example MeGridBehavior (inherited form BaseGridBehavior), MeGridViewMultiComboBoxColumn(inherited form GridViewMultiComboBoxColumn)MeMultiColumnComboBox (inherited form RadMultiColumnComboBox), MeMultiColumnComboBoxElement (inherit RadMultiColumnComboBoxElement)

when init GridView i set this.GridBehavior = new MeGridBehavior(), if column is Combobox editor i create MeGridViewMultiComboBoxColumn column and i  override event  OnEditorRequired and CreateMultiColunmnComboboxElement ;

//On MeMultiColumnCombobox
protected override RadMultiColumnComboBoxElement CreateMultiColumnComboBoxElement()
{
    return new MeMultiColumnComboBoxElement(this.Name,this);
}
 
// on GridView
protected override void OnEditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (this.CurrentColumn is MeGridViewMultiComboBoxColumn)
    {
        var column = this.CurrentColumn as MeGridViewMultiComboBoxColumn;
      MeMultiColumnComboBox cbx= CreateDataComboBox("Countries");
      e.Editor = cbx.MeMultiColumnComboBoxElement;        
    }
}
 
protected override void OnCellValidating(object sender, CellValidatingEventArgs e)
{
    //if value not in list of countries ?
    var cbxCol = e.Column as MeGridViewMultiComboBoxColumn;
    var detectError = ComboBoxValidating(text, e);
    if (detectError)
    {
        e.Cancel = true;
        base.OnCellValidating(sender,e);
        ShowValidateError(); 
        return;
    
}


When i click to another cell it throw exeption on mouseup event of MeGridBehavior 

Unable to cast object of type 'MeMultiColumnComboBoxElement' to type 'Telerik.WinControls.UI.BaseGridEditor'.

   at Telerik.WinControls.UI.GridViewEditManager.EndEditCore(Boolean validate, Boolean cancel)
   at Telerik.WinControls.UI.GridViewEditManager.CloseEditor()
   at Telerik.WinControls.UI.RadGridViewElement.CloseEditor()
   at Telerik.WinControls.UI.GridRowBehavior.OnMouseUpLeft(MouseEventArgs e)
   at Telerik.WinControls.UI.GridRowBehavior.OnMouseUp(MouseEventArgs e)
   at Telerik.WinControls.UI.BaseGridBehavior.OnMouseUp(MouseEventArgs e)
   at ......OnMouseUp(MouseEventArgs e) in d:\\MeProject\GridView\MeGridViewBehavior.cs

This error throw when I update to telerik rad control winforms version Q3 2013  but it work with version Q1 2013

Any one can help me ! 
Thanks .

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Jan 2014, 07:45 AM
Hello Le,

Thank you for contacting Telerik Support.

This is a known issue, concerning our GridViewMultiComboBoxColumn. We have it logged in our Public Issue Tracking System - PITS. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - PITS issue.

Currently, the possible workaround that I can suggest is to set EditorManager.CloseEditorWhenValidationFails to true:
radGridView1.EditorManager.CloseEditorWhenValidationFails = true;

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Le
Top achievements
Rank 1
answered on 15 Jan 2014, 01:59 AM
Hello Desislava.
Thank you very much about support.
Your suggest help for me pass error and not throw exception , but, as you see in my code , onCellFormating event i want if value not in list i will show message and pointer must focus at current cell for end user type new value or select one value in my combobox editor, So, i set  
 CloseEditorWhenValidationFails =true   pointer focus to another cell. Can you help me If you have any idea about my issue,

Thanks .
        
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Jan 2014, 03:41 PM
Hello Le,

Thank you for contacting us again.

I can suggest an alternative solution for the GridViewMultiComboBoxColumn cancelling validation without changing the current focus to the next cell. The sample code uses a custom RadGridView:
private void Form1_Load(object sender, EventArgs e)
{
    this.categoriesTableAdapter.Fill(this.northwindDataSet.Categories);
}
 
private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
    int valueToCancel = 0;
    if (e.Column is GridViewMultiComboBoxColumn && e.ActiveEditor != null && e.ActiveEditor.Value != null)
    {
        if (int.TryParse(e.ActiveEditor.Value.ToString(), out valueToCancel) && valueToCancel == 1)
        {
            e.Cancel = true;
            MessageBox.Show("failed");
        }
    }
}
 
public class CustomGrid : RadGridView
{
    protected override RadGridViewElement CreateGridViewElement()
    {
        return new CustomGridElement();
    }
}
 
public class CustomGridElement : RadGridViewElement
{
    protected override GridViewEditManager CreateEditorManager()
    {
        return new CustomEditManager(this);
    }
}
 
public class CustomEditManager : GridViewEditManager
{
    public CustomEditManager(RadGridViewElement gridViewElement) : base(gridViewElement)
    {
    }
 
    protected override bool EndEditCore(bool validate, bool cancel)
    {
        GridCellElement cell = this.GridViewElement.CurrentView.CurrentCell;
        bool validated = true;
 
        if (validate)
        {
            this.GridViewElement.TableElement.BeginUpdate();
            validated = !RaiseCellValidatingEvent(cell.RowInfo, cell.ColumnInfo,
                this.ActiveEditor.Value, cell.Value);
            this.GridViewElement.TableElement.EndUpdate(false);
            if (!validated && !CloseEditorWhenValidationFails)
            {
                BaseGridEditor baseGridEditor = this.ActiveEditor as BaseGridEditor;
                RadElement element = null;
                if (baseGridEditor == null)
                {
                    element = this.ActiveEditor as RadElement;
                }
                else
                {
                    element = baseGridEditor.EditorElement; 
                }
 
                RadTextBoxItem textBoxItem = element != null ? element.FindDescendant<RadTextBoxItem>() : null;
 
                if (textBoxItem != null)
                {
                    textBoxItem.HostedControl.Focus();
                }
 
                return validated;
            }
            else
            {
                validate = false;
            }
        }
        return base.EndEditCore(validate, cancel);
    }
 
    private bool RaiseCellValidatingEvent(GridViewRowInfo row, GridViewColumn column,
        object newValue, object oldValue)
    {
        CellValidatingEventArgs cellValidating = new CellValidatingEventArgs(row,
            column, newValue, oldValue, ActiveEditor);
        this.GridViewElement.Template.EventDispatcher.RaiseEvent<CellValidatingEventArgs>
            (EventDispatcher.CellValidating, this, cellValidating);
        return cellValidating.Cancel;
    }
}

Please do not hesitate to contact us if you have any additional questions.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Le
Top achievements
Rank 1
answered on 18 Jan 2014, 02:06 AM
Thanks Desislava and Telerik team very much .It's really very helpfull for me.
Best regard!
Tags
GridView
Asked by
Le
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Le
Top achievements
Rank 1
Share this question
or