RadGridViewComboBoxColumn blank. Not showing display member.

4 posts, 1 answers
  1. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 27 Oct 2010 Link to this post

    I have a gridview with a combo box column in it. It is populated with values. When the user changes the display member, the value (id) is committed to the database just fine. When I reopen the datagrid the combobox is blank. The value that was selected previous is not displaying. The values are still in the drop down, but I want the box to show the value that is currently in the database. The picture attached shows what I am talking about. 

  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 27 Oct 2010 Link to this post

    Hello,

    The problem here is that the value from the cell is changing and when you are opening the dropdown the second time, it cannot find a match for the value you currently have selected, that's why it is blank.

    Please take a look at the following example:
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radGridView1 = new RadGridView());
            this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
     
            radGridView1.Dock = DockStyle.Fill;
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.DataSource = new ProductsCollection(1000);
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            radGridView1.Columns["BuyerId"].IsVisible = false;
     
            var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
            column.DataSource = new BuyersCollection(10, 10);
     
            column.ValueMember = "Id";
            column.FieldName = "BuyerId";
     
            column.DisplayMember = "Name";
            this.radGridView1.Columns.Add(column);
     
            this.radGridView1.BestFitColumns();
        }
    }
     
    #region Helpers
     
    public class Product : INotifyPropertyChanged
    {
        private int id, buyerId;
     
        public int BuyerId
        {
            get { return buyerId; }
            set
            {
                buyerId = value;
                OnPropertyChanged("BuyerId");
            }
        }
     
        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged("Id");
            }
        }
     
        public Product(int id, int buyerId)
        {
            this.Id = id;
            this.BuyerId = buyerId;
        }
     
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        public event PropertyChangedEventHandler PropertyChanged;
    }
     
    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(int startIndex, int lenght)
        {
            for (int i = 0; i < 10; i++)
            {
                this.Add(new Buyer(i + 10, "Buyer" + (i + 1)));
            }
        }
    }
     
    #endregion Helpers

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

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. KawaUser
    KawaUser avatar
    57 posts
    Member since:
    Oct 2010

    Posted 28 Oct 2010 Link to this post

    All of the properties for this column were set in the property builder. I did not code any of this... I don't understand why it wouldn't be easier than you made it look. 
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 28 Oct 2010 Link to this post

    Hello again,

    This was just an example and it was meant to see if your problem can be solved with this. I would like to ask you to please try changing your code like this, creating the GridViewComboBoxColumn on DataBindingComplete event and see if it works like this, if it works than the problem is somewhere in the designers connections, if it doesn't than it is something else.

    Or if you can provide a sample form with the designer part also, it would go a lot faster.

    Best Regards,
    Emanuel Varga
Back to Top