RadDropDownListEditor - The cell reverts to the value instead of the text once the selection is made

6 posts, 1 answers
  1. Leigh
    Leigh avatar
    3 posts
    Member since:
    Dec 2013

    Posted 03 Dec 2013 Link to this post

    I am using RadDropDownListEditorWhen the selection is made and dropdown loses focus, the cell reverts from the display text to the value.
    How to keep the display text and not replace the cell with the value once the selection is made? I found an old thread back to 2011 which reported the same issue, however the suggestion does not fit my scenario, as I have to use a GridViewTextBoxColumn.

    Here is the simplified sample of my cs codes. The designer is very simple, just GridViewTextBoxColumn in a RadGridView control. A screenshot of the issue is also attached. 
    public partial class Form1 : RadForm
    {
        public Form1()
        {
            InitializeComponent();
            this.radGridView1.EditorRequired += radGridView1_EditorRequired;
            this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
        }
     
        void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            if (e.ActiveEditor is RadDropDownListEditor)
            {
                RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
                RadDropDownListEditorElement element = (RadDropDownListEditorElement)editor.EditorElement;
                element.DisplayMember = "Description";
                element.ValueMember = "Code";
                element.DataSource = this.CreateDataTable();
                element.SelectedIndex = -1;
            }              
        }
     
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            e.EditorType = typeof(RadDropDownListEditor);
        
         
        private DataTable CreateDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Code", typeof(decimal));
            dataTable.Columns.Add("Description", typeof(string));
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dataTable.NewRow();
                dr[0] = i + 1;
                dr[1] = "Description " + (i + 1);
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }
    }

    Any suggestion is appreciated.

    Regards,
    Shuping
  2. Answer
    George
    Admin
    George avatar
    500 posts

    Posted 06 Dec 2013 Link to this post

    Hello Shuping,

    Thank you for contacting us.

    The display member is used to show some property and the value member is used for some more valuable information. In this case the cell shows the value member. You can either swap your Value/Display members or set them to be the same. If this does not work out for you then your other option is to use the CellFormatting. Below you can find a sample code:
    void grid_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.Tag != null)
        {
            e.CellElement.Value = e.CellElement.Tag;
            e.CellElement.Text = e.CellElement.Tag.ToString();
        }
    }
     
    void grid_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        e.EditorType = typeof(RadDropDownListEditor);
    }
     
    RadDropDownListEditorElement element;
    void grid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (e.ActiveEditor is RadDropDownListEditor)
        {
            RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
            this.element = (RadDropDownListEditorElement)editor.EditorElement;
            this.element.SelectedValueChanged -= element_SelectedValueChanged;
            this.element.SelectedValueChanged += element_SelectedValueChanged;
            this.element.DisplayMember = "Description";
            this.element.ValueMember = "Code";
            this.element.DataSource = this.CreateDataTable();
            this.element.SelectedIndex = -1;
        }          
    }
     
    void element_SelectedValueChanged(object sender, Telerik.WinControls.UI.Data.ValueChangedEventArgs e)
    {
        if (e.Position != -1)
        {
            this.grid.CurrentCell.Tag = (this.element.Items[e.Position].DataBoundItem as DataRowView)[this.element.DisplayMember];
        }
    }
     
    private DataTable CreateDataTable()
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("Code", typeof(decimal));
        dataTable.Columns.Add("Description", typeof(string));
        for (int i = 0; i < 5; i++)
        {
            DataRow dr = dataTable.NewRow();
            dr[0] = i + 1;
            dr[1] = "Description " + (i + 1);
            dataTable.Rows.Add(dr);
        }
     
        return dataTable;
    }

    I hope this helps.

    Regards,
    George
    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 >>
  3. Leigh
    Leigh avatar
    3 posts
    Member since:
    Dec 2013

    Posted 06 Dec 2013 Link to this post

    Hello George,

    Thank you for the reply and assistance. With your suggestion of using CellFormatting and code sample, I am able to apply and adjust it into my project. It solves the problem. 

    Regards,
    Shuping
  4. George
    Admin
    George avatar
    500 posts

    Posted 11 Dec 2013 Link to this post

    Hello Shuping,

    I am glad that this solution worked out for you.

    Do not hesitate to contact us, should you have any further questions.

    Regards,
    George
    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 >>
  5. Leigh
    Leigh avatar
    3 posts
    Member since:
    Dec 2013

    Posted 11 Dec 2013 Link to this post

    Hello George,

    Thank you for the assistance. I have one follow-up question regarding this issue.

    The sample code you provided replaces not only the 
    CellElement.Text but also the CellElement.Value with the tag/description text, which makes the value member and display member to be the same. As a result, when I need to get the cell's value via 

    RadGridView.CurrentCell.Value, it returns me the tag/description text instead of the valuable information which is the code value I need.


    Therefore, I simply comment out the code of setting the 
    CellElement.Value. It works except for the first time selection. Whenever I add a new row and make an initial selection, it displays the value member. After that, if I re-click the same cell and make a selection again, it works fine and displays the display member.

    Do you have any suggestion/solution for making the first time selection also be able to keep the display member? 

    Thank you.
    Shuping

  6. George
    Admin
    George avatar
    500 posts

    Posted 16 Dec 2013 Link to this post

    Hi Shuping,

    Thank you for contacting us.

    Currently, due to the nature of your requirements it is not possible to achieve the desired by you effect in the cleanest way. Your best option would be to have an external dictionary which will hold the values for you. You can change your code to the following:
    private Dictionary<int, object> values = new Dictionary<int, object>();
    void element_SelectedValueChanged(object sender, Telerik.WinControls.UI.Data.ValueChangedEventArgs e)
    {
        if (e.Position != -1)
        {
            this.values[this.grid.Rows.Count] = (this.element.Items[e.Position].DataBoundItem as DataRowView)[this.element.ValueMember];
            this.grid.CurrentCell.Tag = (this.element.Items[e.Position].DataBoundItem as DataRowView)[this.element.DisplayMember];
        }
    }

    This way you can access the value of the cell on a given row. You can also extend this functionality further.

    I hope this helps.

    Regards,
    George
    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 >>
Back to Top