Modify selected item in GridViewComboBoxColumn

9 posts, 1 answers
  1. Bruno
    Bruno avatar
    2 posts
    Member since:
    Mar 2011

    Posted 09 Mar 2011 Link to this post

    Hello,

    I have included a GridViewComboBoxColumn into a RadGridView.
    It has been filled with some strings (let say "1","2","3","4" and "5").

    When the GridView is displayed, the GridViewComboBoxColumn does not show any value. Is it possible to set the selected value (for example, select the third string in the GridViewComboBoxColumn ) at display time so the GridViewComboBoxColumn show "3"  when displaying the grid ?

    I have the same question when the values displayed on my GridView are modified. Can I update the GridViewComboBoxColumn selected item so the selected item is set according to the data values that were modified in my gridview ?

    Can you please give me a code sample for that ? I wish there was a "SelectedItem" property on GridViewComboBoxColumn that I could get and set easily.

    Thank you for your help.

  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 09 Mar 2011 Link to this post

    Hello Bruno,

    Your data source will allow these values in the combo column to be automatically selected for you. Please have a look at this help topic

    In an overview, if you were to have the following combo column defined in your grid

    GridViewComboBoxColumn detailColumn = new GridViewComboBoxColumn();
    detailColumn.FieldName = "DetailId";
    this.radGridView1.Columns.Add(detailColumn);
    detailColumn.DataSource = details;
    detailColumn.Name = "TheDetails";
    detailColumn.DisplayMember = "DetailName";
    detailColumn.ValueMember = "Id";
    and your datasource for your grid contained an "Id" columnm, then it will select the correct value in the list for each record.

    Hope that helps
    Richard

  3. Bruno
    Bruno avatar
    2 posts
    Member since:
    Mar 2011

    Posted 09 Mar 2011 Link to this post


    In fact, in the data of the grid, I have a VAT value in decimal.
    The information displayed in the combo does not come from the same source, but from a small list of VAT rates.

    So when the value in the grid show "19.6", I want the combo to display "Normal rate". When the value in the grid show "5.5", I want the combo to display "Reduced rate" and so on.

    So I don't understand how binding will be able to make a correspondence between the VAT value in the grid and the VAT Label displayed in the combo. That's why I want to be able to set the selected value on the combo, according to the value displayed in the grid.



     

  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 09 Mar 2011 Link to this post

    Hello Bruno,

    Please se this sample. It gives you two options, a standard combo column, or the one I prefer is a GridViewMultiColumnCombo.
    You can paste this directly into a new project

    Designer File

    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();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(469, 262);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(469, 262);
            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();
            this.ResumeLayout(false);
        }
        #endregion
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }

    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;
      
      
        public partial class Form1 : Form
        {
            private BindingList<Product> products = new BindingList<Product>();
            private BindingList<Vat> vatRates = new BindingList<Vat>();
      
            public Form1()
            {
                InitializeComponent();
      
                vatRates.Add(new Vat("Standard", 20));
                vatRates.Add(new Vat("Low Rate", 12));
                vatRates.Add(new Vat("Zero Rate", 0));
      
                products.Add(new Product("Paracetamol", 20));
                products.Add(new Product("Ibuprohen", 12));
                products.Add(new Product("Calpol", 0));
            }
      
            private void Form1_Load(object sender, EventArgs e)
            {
                this.radGridView1.AutoGenerateColumns = false;
                this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
      
                GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
                nameColumn.HeaderText = "Name";
                nameColumn.Name = "Name";
                nameColumn.FieldName = "Name";
      
                GridViewComboBoxColumn rateColumn = new GridViewComboBoxColumn();
                rateColumn.HeaderText = "Vat Rate";
                rateColumn.FieldName = "VatRate";
                rateColumn.DataSource = vatRates;
                rateColumn.DisplayMember = "VatRate";
                rateColumn.ValueMember = "VatRate";
      
                // or use a MultiComboColumn
                GridViewMultiComboBoxColumn rateColumnCombo = new GridViewMultiComboBoxColumn();
                rateColumnCombo.HeaderText = "Vat Rate";
                rateColumnCombo.FieldName = "VatRate";
                rateColumnCombo.DataSource = vatRates;
                rateColumnCombo.DisplayMember = "VatRate";
                rateColumnCombo.ValueMember = "VatRate";
      
                this.radGridView1.Columns.Add(nameColumn);
                this.radGridView1.Columns.Add(rateColumn);
                this.radGridView1.Columns.Add(rateColumnCombo);
      
                this.radGridView1.DataSource = products;
            }
      
            private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                if (e.ActiveEditor is RadMultiColumnComboBoxElement)
                {
                    RadMultiColumnComboBoxElement editor = (RadMultiColumnComboBoxElement)e.ActiveEditor;
                    editor.EditorControl.BestFitColumns();
                }
            }
        }
      
        public class Product
        {
            public Product(string name, decimal vatRate)
            {
                this.Name = name; this.VatRate = vatRate;
            }
      
            public string Name
            { get; set; }
      
            public decimal VatRate
            { get; set; }
        }
      
        public class Vat
        {
            public Vat(string name, decimal vatRate)
            {
                this.Name = name; this.VatRate = vatRate;
            }
      
            public string Name
            { get; set; }
      
            public decimal VatRate
            { get; set; }
        }

    Hope that helps
    Richard

  5. Bruno
    Bruno avatar
    2 posts
    Member since:
    Mar 2011

    Posted 10 Mar 2011 Link to this post


    I appreciate the code sample you sent to me.
    I have to say that as a C++ programmer, I am not used to binding datasources. That's the reason why I had trouble understanding your samples.

    Now I got an explanation about data binding by one of my colleagues, and things become easier to understand.

    Thank you.

  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 10 Mar 2011 Link to this post

    You're welcome Bruno. Glad that I could be of help
    Richard

  7. Dursun
    Dursun avatar
    3 posts
    Member since:
    Oct 2012

    Posted 24 Mar 2013 Link to this post

    i have radgrid in my project . I am tring to bind the grid , in my grid i have combobox column and combobox's column datasource is differernt than the grid's datasource, i can't have grid's combobox item selected,

    GridViewComboBoxColumn blokcolumn = grdOda.Columns["Blok"] as GridViewComboBoxColumn;
                blokcolumn.DataSource = GetBlok();
                blokcolumn.ValueMember = "BlokID";
                blokcolumn.DisplayMember = "BlokName";
     
    //getblok gets the blok list from database , for example: A,B,C,D...  then
     
     
     Classes.ProcessClass.T_Oda odaprocessclass = new Classes.ProcessClass.T_Oda();
                Classes.ModelClass.T_Oda odamodelclass = new Classes.ModelClass.T_Oda();
                odamodelclass.OdaID = 0;
                List<Classes.ModelClass.T_Oda> odalist = odaprocessclass.T_OdaSelect(odamodelclass);
                grdOda.DataSource = odalist;
     
    //oda means in my language  is room (for you to understand me better) when i bind the list odalist to gridview it has a property name BlokID but in the end nothing happens the field is empty all the time?
    i need help 
    Thanks

  8. Ivan Petrov
    Admin
    Ivan Petrov avatar
    691 posts
    Member since:
    Sep 2012

    Posted 25 Mar 2013 Link to this post

    Hello Dursun,

    Thank you for writing.

    I do not see anything wrong with the code you have provided. I have attached an example project where I have implemented your scenario with the grid bound to a list and the combo box column bound to another list. You can see that the values are displayed correctly and when you open the combo for edit the proper item in the drop down is selected.

    I hope this will be useful. Should you need further assistance, I would be glad to help.

    Kind regards,
    Ivan Petrov
    the Telerik team

    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.

  9. Dursun
    Dursun avatar
    3 posts
    Member since:
    Oct 2012

    Posted 26 Mar 2013 Link to this post

    Thank you Ivan Petrov,
    i figured out what my problem is: i was writing the fieldname wrong. I  noticed that with your example :)

Back to Top