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

Show displaymember value instead of valuemember value

6 Answers 205 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Svein Thomas
Top achievements
Rank 1
Svein Thomas asked on 20 Jan 2011, 01:24 PM
Hi guys,

I have a customized grid where i change the type of the editorElement in one cell based on the value on another cell.
One of this editors are the RadDropDownListEditorElement. The displaymembers shows nicly when dropped down.
But when i choose one of the elements in the dropdown list, the dropdown closes and the valuemember is presented in the cell.

I need the valuemember in some operations after this, but i want the cell to show the displaymember. 

Here is the code in the CellEditorInitialized:

void TextCodeGridView_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    var editManager = sender as GridViewEditManager;
    var editor = editManager.ActiveEditor as RadDropDownListEditor;
 
    if (editor == null) return;
 
    var editorElement = ((RadDropDownListEditor)editor).EditorElement;
    switch (e.ColumnIndex)
    {
        case 0:
            var tcArray = _db.TCs.Where(t=>t.TC_GROUP_MEMBERs.Any(tcg=>tcg.TC_GROUP.LAND_IDX == Startup.CurrentConsultant.CountryId)).ToList();
            tcArray.Insert(0, null);
 
            ((RadDropDownListEditorElement)editorElement).DataSource = tcArray;
            ((RadDropDownListEditorElement)editorElement).DisplayMember = "TC1";
            ((RadDropDownListEditorElement)editorElement).ValueMember = "TC1"; ;
             
 
            if (this.TextCodeGridView.CurrentRow.Cells[0].Value == null)
                ((RadDropDownListEditorElement)editorElement).SelectedIndex = 0;
            else
                ((RadDropDownListEditorElement)editorElement).SelectedIndex = tcArray.IndexOf(_db.TCs.SingleOrDefault(t => t != null && t.TC1 == this.TextCodeGridView.CurrentRow.Cells[0].Value));
            break;
        case 1:
            var tclistArray = _db.TC_LISTs.Where(t => t != null && t.TC == this.TextCodeGridView.CurrentRow.Cells[0].Value).ToList();
            tclistArray.Insert(0, null);
 
            ((RadDropDownListEditorElement)editorElement).DataSource = tclistArray;
            ((RadDropDownListEditorElement)editorElement).DisplayMember = "TEXT";
            ((RadDropDownListEditorElement)editorElement).ValueMember = "VALUE"; ;
 
            ((RadDropDownListEditorElement)editorElement).SelectedIndex = tclistArray.IndexOf(tclistArray.SingleOrDefault(t => t != null && t.ISDEFAULT));
            this.TextCodeGridView.CurrentRow.Cells[1].Value = tclistArray.SingleOrDefault(t => t != null && t.ISDEFAULT).TEXT;
             
            break;
    }
}

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 20 Jan 2011, 01:36 PM
Hi Sven,

A suggestion would be to swap the valuemember in this way
((RadDropDownListEditorElement)editorElement).ValueMember = "TEXT";
 and then you could always store the value in the tag property for later inspection.

hope that helps
Richard
0
Svein Thomas
Top achievements
Rank 1
answered on 21 Jan 2011, 10:34 AM
Hi again, 

This is what i initially had done. How can i add the VALUE into the TAG if I only have the TEXT and the TEXT is not unique in the database table. 
And why is the ValueMember shown in the cell?

Regards
Svein Thomas
0
Richard Slade
Top achievements
Rank 2
answered on 21 Jan 2011, 11:42 AM
Hi Sven,

Even though this is not exactly replicating your exmaple, it does show that you can set the display as one value, and using CellFormatting, set the tag to another value.

Designer File
namespace RadGridView_Basic_C
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            this.radButton1 = new Telerik.WinControls.UI.RadButton();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(457, 349);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.radGridView1_CellFormatting);
            // 
            // radButton1
            // 
            this.radButton1.Location = new System.Drawing.Point(278, 368);
            this.radButton1.Name = "radButton1";
            this.radButton1.Size = new System.Drawing.Size(130, 24);
            this.radButton1.TabIndex = 1;
            this.radButton1.Text = "radButton1";
            this.radButton1.Click += new System.EventHandler(this.radButton1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(457, 411);
            this.Controls.Add(this.radButton1);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
        private Telerik.WinControls.UI.RadButton radButton1;
    }
}

Form1.cs
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;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI.Export;
using Telerik.WinControls.UI.Export.HTML;
using Telerik.WinControls.RadControlSpy;
  
  
namespace RadGridView_Basic_C
{
    public partial class Form1 : Form
    {
  
        private List<Person> m_myList = new List<Person>();
        List<City> m_Cities = new List<City>();
  
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
  
  
            this.radGridView1.AutoGenerateColumns = false;
            this.radGridView1.AllowAddNewRow = true;
            this.radGridView1.AllowEditRow = true;
  
            m_myList.Add(new Person(10, "Richard", "Bournemouth"));
            m_myList.Add(new Person(20, "Stew", "Christchurch"));
            m_myList.Add(new Person(30, "Chris", "Bearwood"));
            m_myList.Add(new Person(40, "Peter", "Southampton"));
            radGridView1.DataSource = m_myList;
  
            m_Cities.Add(new City(1, "Bournemouth"));
            m_Cities.Add(new City(2, "Southampton"));
            m_Cities.Add(new City(3, "Bearwood"));
            m_Cities.Add(new City(4, "Christchurch"));
  
  
            GridViewDecimalColumn idColumn = new GridViewDecimalColumn();
            idColumn.Name = "Id";
            idColumn.HeaderText = "Id";
            idColumn.FieldName = "Id";
            this.radGridView1.Columns.Add(idColumn);
  
            GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
            nameColumn.Name = "Name";
            nameColumn.HeaderText = "Name";
            nameColumn.FieldName = "Name";
            this.radGridView1.Columns.Add(nameColumn);
  
            GridViewComboBoxColumn cityColumn = new GridViewComboBoxColumn();
            cityColumn.Name = "City";
            cityColumn.HeaderText = "City";
            cityColumn.FieldName = "City";
            this.radGridView1.Columns.Add(cityColumn);
  
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized);
  
  
  
        }
  
        void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            var editManager = sender as GridViewEditManager;
            var editor = editManager.ActiveEditor as RadDropDownListEditor;
  
            if (editor == null) return;
  
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)((RadDropDownListEditor)editor).EditorElement;
  
            editorElement.DropDownStyle = RadDropDownStyle.DropDown;
            editorElement.DataSource = m_Cities;
            editorElement.DisplayMember = "Name";
            editorElement.ValueMember = "Name";
        }
  
  
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "City")
            {
                if (e.Row.Cells["City"].Value != null)
                
                     foreach (City c in m_Cities)
                    {
                        if (c.Name == e.Row.Cells["City"].Value.ToString())
                        {
                            e.Row.Cells["City"].Tag = c.Id; break
                        }
                    }               
                }
            }
        }
  
        private void radButton1_Click(object sender, EventArgs e)
        {
               MessageBox.Show(this.radGridView1.CurrentRow.Cells["City"].Value.ToString() + " " + this.radGridView1.CurrentRow.Cells["City"].Tag.ToString());
        }
  
  
        class Person
        {
            public Person()
            { }
  
            public Person(int id, string name, string city)
            {
                this.Id = id;
                this.Name = name;
                this.City = city;
            }
  
            public int Id { get; set; }
            public string Name { get; set; }
            public string City { get; set; }
        }
  
        class City
        {
            public City()
            { }
  
            public City(int id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
  
            public int Id { get; set; }
            public string Name { get; set; }
        }
  
    }
}

Hope that helps
Richard
0
Alexander
Telerik team
answered on 25 Jan 2011, 01:07 PM
Hello Svein,

As far as I can see from your code snippet, you perform customization of the column's editor. When you close the editor, the value of the cell depends on the properties of its GridViewComboBoxColumn.

Your case could be implemented in the following way:
1. Create GridViewComboBoxColumn and set DataSource to it. The DataSource should contain all possible data, which will be used in the differently customized editors of the column.
2. Set the column's ValueMember and DisplayMember.
3. Perform your logic in the CellEditorInitialized event handler to customize the editor of the column.

I hope it will help you to achieve your scenario.

Best regards,
Alexander
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
Michael
Top achievements
Rank 1
Veteran
answered on 03 Nov 2020, 11:54 AM

As far as I can tell from the Telerik replies here, DisplayMember and ValueMember don't really work.  If they did, the Text property would always contain what's in the DisplayMember and the Value property would always contain what's in the ValueMember.  Instead, we're told to store the ValueMember in the Tag so as to have it handy.  Have I misunderstood?

I got here because I've been having similar problems.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Nov 2020, 01:28 PM
Hello, Michael, 

This seems to be quite an old forum post. Please have in mind that GridViewComboBoxColumn displays a set of predefined text values in a drop down list. This column type is typically used to provide a lookup into some set of relatively static values. The DisplayMember controls which property value of the applied DataSource to be displayed in the cells. The ValueMember determines which property value to be stored as a value in the cells. The following help article demonstrates how to setup such a column in RadGridView: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/column-types/gridviewcomboboxcolumn 

If you are still experiencing any further difficulties, it would be greatly appreciated if you can submit a support ticket from your Telerik account and provide more information about the exact setup that you have on your end and what is the exact requirement that you are trying to achieve. Thus, we would be able to get better understanding of the precise case and provide further assistance. Thank you in advance.

I hope this information helps. If you need any further assistance please don't hesitate to contact me.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Svein Thomas
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Svein Thomas
Top achievements
Rank 1
Alexander
Telerik team
Michael
Top achievements
Rank 1
Veteran
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or