GridViewComboBoxColumn Query

6 posts, 1 answers
  1. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jun 2011

    Posted 01 Oct 2013 Link to this post

    If have some related data in a couple of  IList<> that I have read from SQL Server using Fluent NHibernate.

    If have object of the following class in a list
    public class Media
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual decimal Halo { get; set; }
        public virtual decimal ExchangeRate { get; set; }
        public virtual int DecimalPlaces { get; set; }
        public virtual bool IsCash { get; set; }
        public virtual MediaProperty Property { get; set; }
    }

    and I have objects of this class in a list also

    public class MediaProperty
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual bool InUse { get; set; }
        public virtual bool IsForeign { get; set; }
        public virtual bool TenderingProhibited { get; set; }
        public virtual bool OverTenderingProhibited { get; set; }
        public virtual bool TenderingZeroBalanceProhibited { get; set; }
        public virtual bool NoChangeFromThisMedia { get; set; }
        public virtual bool NoChangeGiven { get; set; }
        public virtual bool TenderingCompulsory { get; set; }
        public virtual bool ChangeGoesToTips { get; set; }
        public virtual bool ChangeFromSelectedMedia { get; set; }
        public virtual Enumerations.CidAffect AffectsCid { get; set; }
        public virtual Enumerations.DrawerOpen OpensDrawer { get; set; }
    }

    In my WinForms application I want to edit the MediaProperty value in a drop down list, and select the MediaProperty by the Name field in the list.  The RadGridView has its DataSource property set as the list of Media,  how do I configure the GridViewComboBoxColumn to provide the functionality I want.


    Thanks in advance
  2. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jun 2011

    Posted 02 Oct 2013 Link to this post

    I have tried this code :
    gridData.AutoGenerateColumns = false;
     
    // Set the datasource to point at the data
    gridData.DataSource = _medias;
     
    var col = new GridViewTextBoxColumn("Id");
    col.HeaderText = "Id";
    col.ReadOnly = true;
    col.Width = 32;
    gridData.Columns.Add(col);
     
    col = new GridViewTextBoxColumn("Name");
    col.HeaderText = "Name";
    col.Width = 150;
    gridData.Columns.Add(col);
     
    col = new GridViewTextBoxColumn("Halo");
    col.HeaderText = "HALO";
    col.Width = 100;
    col.FormatString = "{0:C}";
    gridData.Columns.Add(col);
     
    var decCol = new GridViewDecimalColumn("ExchangeRate");
    decCol.HeaderText = "Exchange Rate";
    decCol.Width = 80;
    decCol.DecimalPlaces = 5;
    gridData.Columns.Add(decCol);
     
    col = new GridViewTextBoxColumn("DecimalPlaces");
    col.HeaderText = "Decimal Places";
    col.Width = 80;
    col.FormatString = "{0}";
    col.TextAlignment = ContentAlignment.MiddleRight;
    gridData.Columns.Add(col);
     
    var colChk = new GridViewCheckBoxColumn("IsCash");
    colChk.HeaderText = "Is Cash";
    colChk.Width = 64;
    gridData.Columns.Add(colChk);
     
    var colDrop = new GridViewComboBoxColumn("Property");
    colDrop.DataSource = _mediaProps;
    colDrop.ValueMember = "Id";
    colDrop.DisplayMember = "Name";
    colDrop.HeaderText = "Property";
    colDrop.Width = 100;
    gridData.Columns.Add(colDrop);
     
    // Setup various events
    gridData.RowsChanged += gridData_RowsChanged;
    gridData.DefaultValuesNeeded += gridData_DefaultValuesNeeded;
    gridData.UserDeletingRow += gridData_UserDeletingRow;


    The addition of the GridViewComboBoxColumn causes an exception:
    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Object must implement IConvertible.
       at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
       at Telerik.WinControls.UI.RadDataConverter.ParseCore(Object value, Type targetType, Type sourceType, TypeConverter dataTypeConverter, IDataConversionInfoProvider dataColumn, Boolean checkFormattedNullValue)
       at Telerik.WinControls.UI.RadDataConverter.Parse(IDataConversionInfoProvider converstionInfoProvider, Object value)
       at Telerik.WinControls.UI.GridViewComboBoxColumn.GetLookupValue(Object cellValue)
       at Telerik.WinControls.UI.GridComboBoxCellElement.SetContent()
       at Telerik.WinControls.UI.GridVirtualizedCellElement.Initialize(GridViewColumn column, GridRowElement row)

    Any ideas how to progress this?
  3. George
    Admin
    George avatar
    500 posts

    Posted 04 Oct 2013 Link to this post

    Hello Jonathan,

    Thank you for contacting us.

    Your approach with the manually added columns is correct. However you need to change the name of the GridViewComboBoxColumn to something different from "Property" since the grid will try to match that with the property from your Media class. You can track for changes in the combo box and set the Property of the Media class according to the selected value:
    public partial class Form1 : Form
    {
        private RadGridView grid = new RadGridView();
        private BindingList<Media> media = new BindingList<Media>();
        private BindingList<MediaProperty> properties = new BindingList<MediaProperty>();
     
        public Form1()
        {
            InitializeComponent();
     
            this.Controls.Add(this.grid);
            this.grid.Dock = DockStyle.Fill;
            this.grid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.grid.AutoGenerateColumns = false;
            this.grid.DataSource = this.media;
     
            this.grid.Columns.Add(new GridViewDecimalColumn("Id"));
            this.grid.Columns.Add(new GridViewTextBoxColumn("Name"));
            GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("Properties");
            this.grid.Columns.Add(comboColumn);
     
            comboColumn.DataSource = this.properties;
            comboColumn.DisplayMember = "Name";
            comboColumn.ValueMember = "Name";
     
            for (int i = 0; i < 10; i++)
            {
                this.media.Add(new Media
                {
                    Id = i,
                    Name = "Name " + i
                });
     
                this.properties.Add(new MediaProperty
                {
                    Id = i,
                    Name = "Prop Name " + i
                });
            }
     
            this.grid.CellValueChanged += grid_CellValueChanged;
        }
     
        void grid_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            if (e.Column is GridViewComboBoxColumn)
            {
                object cellValue = this.grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                (e.Row.DataBoundItem as Media).Property = this.properties.FirstOrDefault(x => x.Name == cellValue);
            }
        }
    }
     
    public class MediaProperty
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
     
    public class Media
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual MediaProperty Property { get; set; }
    }

    I hope this helps. Let me know if you need any further assistance.

    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 >>
  4. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jun 2011

    Posted 04 Oct 2013 Link to this post

    George,

    Thank you for your help.  Your solution does allow me to make the selection of the MediaProperty against each of the Media objects.

    However when I display the form the existing value of the data is not displayed in the drop down list

    Jonathan
  5. Answer
    George
    Admin
    George avatar
    500 posts

    Posted 09 Oct 2013 Link to this post

    Hello Jonathan,

    Thank you for replying.

    Since the GridViewComboBoxColumn's data source and the grid's data source are two different collections, they are not connected, that is why you will need to manually show the default value. This can be done in the CellFormatting event of the grid:
    void grid_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Column is GridViewComboBoxColumn && e.Row is GridViewDataRowInfo)
        {
            Media media = e.Row.DataBoundItem as Media;
            e.CellElement.Text = (media.Property + "").ToString();
        }
    }

    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 >>
  6. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jun 2011

    Posted 10 Oct 2013 Link to this post

    That's great !!!
    Thank you for your help George
Back to Top