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

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

5 Answers 327 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Leigh
Top achievements
Rank 1
Leigh asked on 03 Dec 2013, 09:45 PM
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

5 Answers, 1 is accepted

Sort by
0
Accepted
George
Telerik team
answered on 06 Dec 2013, 01:38 PM
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 >>
0
Leigh
Top achievements
Rank 1
answered on 06 Dec 2013, 05:04 PM
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
0
George
Telerik team
answered on 11 Dec 2013, 12:00 PM
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 >>
0
Leigh
Top achievements
Rank 1
answered on 11 Dec 2013, 03:54 PM

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

0
George
Telerik team
answered on 16 Dec 2013, 02:53 PM
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 >>
Tags
GridView
Asked by
Leigh
Top achievements
Rank 1
Answers by
George
Telerik team
Leigh
Top achievements
Rank 1
Share this question
or