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

Problem with comb box column if data source is List<T>, 2010 Q2 SP2.

2 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 04 Oct 2010, 04:18 PM

Problem with comb box column if data source is List<T>, 2010 Q2 SP2.

Hi

I set (radGridView1.Columns[0] as GridViewComboBoxColumn).DataSource on List<MyObject>.
During moving focus to another cell (after selecting something from combo box) I get exception.
DisplayMemeber and ValueMember are null – but I think it is ok for this case.


Stack trace:

 

FormatException: Input string was not in a correct format.

   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)

   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)

   at System.ComponentModel.BaseNumberConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)

   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.GridDataCellElement.set_Value(Object value)

   at Telerik.WinControls.UI.GridViewEditManager.EndEditCore(Boolean validate, Boolean cancel)

   at Telerik.WinControls.UI.GridViewEditManager.CloseEditor()

   at Telerik.WinControls.UI.GridViewEditManager.OnPositionChanging(PositionChangingEventArgs args)

   at Telerik.WinControls.UI.GridViewEditManager.Telerik.WinControls.UI.IGridViewEventListener.PreProcessEvent(GridViewEvent eventData)

   at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessCollection(GridViewEvent gridEvent, PriorityWeakReferenceList list, GridEventProcessMode processMode)

   at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessEvent(GridViewEvent gridEvent)

   at Telerik.WinControls.UI.GridViewSynchronizationService.NotifyListeners(GridViewEvent gridEvent)

   at Telerik.WinControls.UI.GridViewSynchronizationService.FlushEvents()

   at Telerik.WinControls.UI.GridViewSynchronizationService.DispatchEvent(GridViewEvent gridEvent)

   at Telerik.WinControls.UI.GridViewSynchronizationService.RaiseCurrentChanged(GridViewTemplate template, GridViewRowInfo row, GridViewColumn column, Boolean user)

   at Telerik.WinControls.UI.BaseGridNavigator.SelectCore(GridViewRowInfo row, GridViewColumn column)

   at Telerik.WinControls.UI.BaseGridNavigator.SelectNextColumn()

   at Telerik.WinControls.UI.GridRowBehavior.ProcessTabKey(KeyEventArgs keys)

   at Telerik.WinControls.UI.GridNewRowBehavior.ProcessTabKey(KeyEventArgs keys)

   at Telerik.WinControls.UI.GridRowBehavior.ProcessKey(KeyEventArgs keys)

   at Telerik.WinControls.UI.BaseGridBehavior.ProcessKey(KeyEventArgs keys)

   at Telerik.WinControls.UI.RadGridView.ProcessDialogKey(Keys keyData)

   at System.Windows.Forms.Control.PreProcessMessage(Message& msg)

   at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)

   at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)



I do not have this problem if I set DataSource to List<string>.

How can I set List< MyObject> as DataSource for comb box column?

Regards

2 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 04 Oct 2010, 04:42 PM
Hi, 

To bind anything to a generic list you'd need to provide public properties to the items that will be the Value Member and Display member. 
E.g. For a RadDropDownList

Public Class Car
    Private m_Make As String
    Private m_Model As String
 
    Public Sub New(ByVal make As String, ByVal model As String)
        m_Make = make
        m_Model = model
    End Sub
 
    Public ReadOnly Property Make() As String
        Get
            Return m_Make
        End Get
    End Property
    Public ReadOnly Property Model() As String
        Get
            Return m_Model
        End Get
    End Property
 
End Class

Dim list As New System.Collections.Generic.List(Of Car)
list.Add(New Car("BMW", "530d"))
list.Add(New Car("Audi", "S6"))
list.Add(New Car("Merc", "SL55"))
list.Add(New Car("Hilman", "Imp"))
Me.RadDropDownList1.DataSource = list
Me.RadDropDownList1.ValueMember = "Model"
Me.RadDropDownList1.DisplayMember = "Make"

Hope that helps
Richard
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 04:46 PM
Hello Raymond,

But did you set the DisplayMember and ValueMember for the column at hand?

Please try this following example and tell me what is different in your case:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace TestGridComboBoxColumnListSource
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var radGridView1 = new RadGridView();
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
            this.Controls.Add(radGridView1);
 
            radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            radGridView1.DataSource = LoadDataSource(20);
        }
 
        private List<Product> LoadDataSource(int noItems)
        {
            var products = new List<Product>();
            for (int i = 1; i <= noItems; i++)
            {
                products.Add(new Product(i, i + 100));
            }
 
            return products;
        }
 
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            var grid = (RadGridView)sender;
            grid.Columns["CategoryId"].IsVisible = false;
 
            var column = new GridViewComboBoxColumn("Categories");
            column.DataSource = LoadCategories(30);
            column.DisplayMember = "Name";
            column.ValueMember = "Id";
            column.FieldName = "CategoryId";
            grid.Columns.Add(column);
 
            grid.BestFitColumns();
        }
 
        private object LoadCategories(int noItems)
        {
            var categories = new List<Category>();
            for (int i = 1; i <= noItems; i++)
            {
                categories.Add(new Category(i + 100, "Category " + i + 100));
            }
 
            return categories;
        }
    }
 
    public class Product
    {
        public int Id
        {
            get;
            set;
        }
 
        public int CategoryId
        {
            get;
            set;
        }
 
        public Product(int id, int categoryId)
        {
            this.Id = id;
            this.CategoryId = categoryId;
        }
    }
 
    public class Category
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public Category(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

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

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
Share this question
or