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

Problem with EditorRequired and CellBeginEdit – cannot set DropDownStyle

15 Answers 380 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 02 Oct 2010, 11:37 AM

 

Hi

 

            Version: Q2 2010 SP2

            I want use custom editor.

            So in EditorRequired handler I create RadDropDownListEditor

            and in CellBeginEdit I set data source for dropdownlist.

 

In CellBeginEdit handler I want also set DropDownStyle on DropDownList but I get exception from telerik source code NullReferenceException.

 


If I do not change DropDownStyle property everything looks ok.


I tried set DropDownStyle on DropDownList in EditorRequired handler but I also got the same exception.

 


I cannot use just combo box type of column – in the same column I need display different editors.

 


Check my short source code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
namespace GridViewComboBox
{
    public partial class Form1 : Form
    {
        private CompoundAssignmentExpression _list = new CompoundAssignmentExpression();
  
        public Form1()
        {
            InitializeComponent();
  
            SimpleAssignmentExpression sae = new SimpleAssignmentExpression();
            sae.AttributeName = "Test";
            sae.Value = "123";
            _list.Add(sae);
  
            sae = new SimpleAssignmentExpression();
            sae.AttributeName = "Test1";
            sae.Value = "xx";
  
            _list.Add(sae);
            bsCAE.DataSource = _list;           
        }
  
        private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            RadDropDownListEditor comboBoxEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
  
            if (comboBoxEditor != null
            {
                RadDropDownListEditorElement editorElement = comboBoxEditor.EditorElement as RadDropDownListEditorElement;
                editorElement.DataSource = new string[] { "aaa", "bbb", "ccc", "ddd" };
  
                //these lines cause exception:
                //editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                //comboBoxEditor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
            }
        }
  
        private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if ((radGridView1.CurrentRow.Index == 0) && (radGridView1.CurrentCell.ColumnIndex == 0))
            {
                RadDropDownListEditor editor = new RadDropDownListEditor();
                //exception here:
                //editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                e.EditorType = typeof(RadDropDownListEditor);
                e.Editor = editor;          
  
            }
        }
    }
}

 


Regards

 

15 Answers, 1 is accepted

Sort by
0
Raymond
Top achievements
Rank 1
answered on 02 Oct 2010, 12:10 PM

ok I solved this problem
I used RadComboBoxEditor - I can set for this class DropDownStyle
on DropDownList.

But this was weird becasue GridViewComboBoxColumn uses RadDropDownListEditor as editor.

0
Martin Vasilev
Telerik team
answered on 07 Oct 2010, 08:38 AM
Hi Raymond,

Thank you for writing.

I am unable to reproduce the described issues when setting DropDownStyle property. The following code works fine on my side with latest official version Q2 2010 SP2 :
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
       {
           RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
           if (editor != null)
           {
               RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
               editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
           }
       }

If you do not use the latest version of RadControls, please download it and give it a try. If you still experience any issues please send me a small sample project which demonstrates your scenario. This will allow me to investigate the case further.

Greetings,
Martin Vasilev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 09:39 AM
Hello,

The exception is happening here:
void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (radGridView1.CurrentCell.ColumnInfo.Name == "SomeComboboxColumn")
    {
        RadDropDownListEditor editor = new RadDropDownListEditor();
        var editorElement = editor.EditorElement as RadDropDownListEditorElement;
        if (editorElement != null)
        {
            editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        }
 
        e.EditorType = typeof(RadDropDownListEditor);
        e.Editor = editor;          
    }
}

and / or here:

void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
    if (editor != null)
    {
        var editorElement = editor.EditorElement as RadDropDownListEditorElement;
        if (editorElement != null)
        {
            editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        }
    }
}

And it is very strange because it is setting the style, but it's throwing a Null Reference... and this happens JUST for DropDownList style, for DropDown everything is OK.

*Update: For anyone interested you can use this as a workaround:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            RadComboBoxEditor editor = this.radGridView1.ActiveEditor as RadComboBoxEditor;
            if (editor != null)
            {
                var editorElement = editor.EditorElement as RadComboBoxEditorElement;
                if (editorElement != null)
                {
                    editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                }
            }
        }
 
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (radGridView1.CurrentCell.ColumnInfo.Name == "SomeComboboxColumn")
            {
                RadComboBoxEditor editor = new RadComboBoxEditor();
                var editorElement = editor.EditorElement as RadComboBoxEditorElement;
                if (editorElement != null)
                {
                    editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                }
 
                e.EditorType = typeof(RadComboBoxEditorElement);
                e.Editor = editor;
            }
        }

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 07 Oct 2010, 10:26 AM

 I use 2010 Q2 SP2

This workaround is not perfect:

  1. When cell is selected and I want expand combo box, very often combo box is expanded and immediately collapsed. User cannot select value from list. I had very similar problem with column GridViewComboBoxColumn in 2010 Q2 SP1.

 

2.   Combo box is not automatically expanded after cell click when previously was selected some another cell – in this way works  
      column GridViewComboBoxColumn.
      I cannot get the same behavior when I use custom editor. 

In CellBeginEdit I call RadComboBoxEditorElement.ShowPopup() but it does not work.

 

 

3.   Background of selected cell is pink/yellow – it should be white.

 

 



Do you have any ideas?

Regards

0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 01:11 PM
Hello Raymond

1 & 2. This was always an issue, at least in my point of view with the ComboBox, because if you just try this test in an application, you will see that the dropdown for the DropDownList stays open while as the dropdown for the ComboBox closes:
var dropDownList = new RadDropDownList();
dropDownList.Dock = DockStyle.Top;
dropDownList.DataSource = some data source;
this.Controls.Add(dropDownList);
 
dropDownList.ShowDropDown();
 
var comboBox = new RadComboBox();
comboBox.Dock = DockStyle.Bottom;
comboBox.DataSource = same data source as the dropdown;
this.Controls.Add(comboBox);
 
comboBox.ShowDropDown();

3. That could be just a strange theme issue, i will look into it, but the 2 points are the most important ones..

Best Regards,
Emanuel Varga
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 01:44 PM
Hello again,

Raymond, please try the following code, from my test i can see that it is working fine:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
            if (editor != null)
            {
                var editorElement = editor.EditorElement as RadComboBoxEditorElement;
                if (editorElement != null)
                {
                    editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                }
            }
        }
 
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (radGridView1.CurrentCell.ColumnInfo.Name == "SomeComboboxColumn")
            {
                e.EditorType = typeof(RadDropDownListEditor);
            }
        }

It's still strange that i had to cast to ComboBoxEditorElement instead of DropDownListEditorElement, but it's working, please let me know if this is OK for you.

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 07 Oct 2010, 01:55 PM


It does not work in my app.

1.  In EditorRequired handler I set

e.EditorType = typeof(RadDropDownListEditor);

2.  In CellBeginEdit handler I do the same as you

var editorElement = comboBoxEditor.EditorElement as RadComboBoxEditorElement;

and editorElement is NULL L


I use version 2010.2.10.914.

 

0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 02:05 PM
Hello again,

You're right, it's null, i think I'm too tired today, but i was assuming it was set, but in my test application you cannot type inside the DropDownListEditor, not even when it's in DropDown mode

void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
            if (editor != null)
            {
                var editorElement = editor.EditorElement as RadDropDownListEditorElement;
                if (editorElement != null)
                {
                    try
                    {
                        editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

** Stupid workaround but altough it is throwing an exception it IS changing the drop down Style

Just as a fun fact, you can change the value from the visual studio debugger from DropDown to DropDownLIst, and it is changing it without any exception.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 07 Oct 2010, 09:23 PM

This workaround is still not enough good.

When I first time expand comb box everything is ok, but if I select some another cell and return to cell with comb box that was previously expanded this combo box does not have any items! L

I don`t know what to do with this simple use case.

0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 10:58 PM
Hello again Raymond,

This does not happen in my test application, please test the following application and tell me what is different in your approach
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1 = new RadGridView();
 
    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        radGridView1.Dock = DockStyle.Fill;
 
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
        this.radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        this.radGridView1.DataSource = new ProductsCollection(1000);
        this.Controls.Add(radGridView1);
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        radGridView1.Columns["BuyerId"].IsVisible = false;
 
        var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
        column.DataSource = new BuyersCollection();
        column.ValueMember = "Id";
        column.FieldName = "BuyerId";
        column.DisplayMember = "Name";
        this.radGridView1.Columns.Add(column);
 
        this.radGridView1.BestFitColumns();
    }
 
    void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            var editorElement = editor.EditorElement as RadDropDownListEditorElement;
            if (editorElement != null)
            {
                try
                {
                    editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                }
                catch (Exception)
                {
                }
            }
        };
    }
 
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (radGridView1.CurrentCell.ColumnInfo.Name == "SomeComboboxColumn")
        {
            e.EditorType = typeof(RadDropDownListEditor);
        }
    }
}
 
#region Helpers
 
public class Product
{
    public int Id
    {
        get;
        set;
    }
 
    public int BuyerId
    {
        get;
        set;
    }
 
    public Product(int id, int buyerId)
    {
        this.Id = id;
        this.BuyerId = buyerId;
    }
}
 
public class Buyer
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public Buyer(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
public class ProductsCollection : BindingList<Product>
{
    public ProductsCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new Product(i, i + 10));
        }
    }
}
 
public class BuyersCollection : BindingList<Buyer>
{
    public BuyersCollection()
    {
        for (int i = 0; i < 10; i++)
        {
            this.Add(new Buyer(i + 10, "Buyer" + (i + 1)));
        }
    }
}
 
#endregion Helpers

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 08 Oct 2010, 07:34 AM

In your example you used GridViewComboBoxColumn – it does not satisfy my needs.

I need to have in one column for some rows simple cell (text box) and for other rows combo box in cell.

In your example in all rows for column GridViewComboBoxColumn there is combo box.
I used GridViewTextBoxColumn and for some rows I want have combo box for this column.

0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 08 Oct 2010, 09:00 AM
Hello again Raymond,

Hope this time everything will be OK...
Please try this, it works in my tests with a GridTextBoxColumn:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            RadDropDownListEditor editor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
            if (editor != null)
            {
                var editorElement = editor.EditorElement as RadDropDownListEditorElement;
                if (editorElement != null)
                {
                    editorElement.DataSource = products;
                    editorElement.DisplayMember = "Name";
                    editorElement.ValueMember = "Name";
                }
            }
        }
 
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (radGridView1.CurrentCell.ColumnInfo.Name == "Name" && radGridView1.CurrentRow.Index % 2 == 0)
            {
                var editor = new RadDropDownListEditor();
                var editorElement = editor.EditorElement as RadDropDownListEditorElement;
                try
                {
                    editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
                }
                catch (Exception)
                {
                }
 
                e.Editor = editor;
                e.EditorType = typeof(RadDropDownListEditor);
            }
        }

Please let me know if this is working for you...

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 08 Oct 2010, 03:20 PM

Fantastic, it works!

Thank you very much!

But you could fix this problem with exception – source code with empty catch does not look professional.

 

0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Oct 2010, 03:22 PM
Hello Raymond,

I know, but sadly I'm not the one who can fix it, just glad to be able to help.

Best Regards,
Emanuel Varga
0
Martin Vasilev
Telerik team
answered on 12 Oct 2010, 03:44 PM
Hello guys,

I managed to reproduce the described exception. We are going to address it in the next major release. Thank you for your cooperation.

Best wishes,
Martin Vasilev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Raymond
Top achievements
Rank 1
Martin Vasilev
Telerik team
Emanuel Varga
Top achievements
Rank 1
Share this question
or