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

Changing Microsoft combobox to Telerik DropDownList

14 Answers 390 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Robert Stuczynski
Top achievements
Rank 1
Robert Stuczynski asked on 28 Sep 2010, 12:48 PM

Hi

I have tried change Microsoft combobox to Telerik new drop down list.

I have used this code to populate Microsoft combobox:

1.cbKlien.Items.Clear(); 
2.var c =bS.view_Trans_Order_Custs.OrderBy(i => i.CustomerCode).Select( 
4.      i => new {i.CustomerCode, i.CompleteCustomerName});
5. 
6.cbKlien.Items.AddRange(c.ToArray()); 
7.cbKlien.DisplayMember = "CustomerCode";

It worked and was very fast.

But when I have added telerik drop down list I have can not populate using the some code :( I have received errors:

1.Error   1   The best overloaded method match for 'Telerik.WinControls.UI.RadListDataItemCollection.AddRange(System.Collections.Generic.IEnumerable<Telerik.WinControls.UI.RadListDataItem>)' has some invalid arguments c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100924 ZlecTrans Test od Telerik\ZlecTransp001\fMain.cs  219 10  Zlecenia transportowe

1.Error   2   Argument '1': cannot convert from 'AnonymousType#1[]' to 'System.Collections.Generic.IEnumerable<Telerik.WinControls.UI.RadListDataItem>'   c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100924 ZlecTrans Test od Telerik\ZlecTransp001\fMain.cs  219 34  Zlecenia transportowe

This error was on this line:

1.ddlKlien.Items.AddRange(c.ToArray());

Of course I can populate using for but it very slow:

1.ddlKlien.Items.Clear();
2.        var c = dbS.view_Trans_Order_Custs.OrderBy(i => i.CustomerCode).Select(
3.              i => new {i.CustomerCode, i.CompleteCustomerName});
4.          
5.        for (int x = 0; x < c.ToArray().Length; x++) ddlKlien.Items.Add(c.ToArray()[x].CustomerCode);
6.        //ddlKlien.Items.AddRange(c.ToArray());
7.        ddlKlien.DisplayMember = "CustomerCode";

Could you help me correct my code for fast speed?

14 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Sep 2010, 09:10 PM
Hello Robert,

Did you try just setting the c as a data source for the DropDownList?
var c = bS.view_Trans_Order_Custs.OrderBy(i => i.CustomerCode).Select(
      i => new {i.CustomerCode, i.CompleteCustomerName});
  
cbKlien.DataSource = c;
cbKlien.DisplayMember = "CustomerCode";

Best Regards,
Emanuel Varga
0
Robert Stuczynski
Top achievements
Rank 1
answered on 29 Sep 2010, 08:58 AM

Thanks for you answer but:

cbKlien.DataSource = c;

OK it work. But it is not fine for me. I prefer populate DropDownList only rather then connect DropDownList using DataSource with base. I am not sure it is clear or not because I am not professional programmer.

For example if I populate using ddlKlien.Items.AddRange(c.ToArray()); I can use ddlKlient.Items.Clear(); or add easy next data (without adding to database).

Do you know how can I do it

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 09:19 AM
Hello again Robert,

Ok, if you prefer doing things like this, you can use:
ddlKlien.Items.Clear();
var c = dbS.view_Trans_Order_Custs.OrderBy(i => i.CustomerCode).Select(
     i => new RadListDataItem(i.CompleteCustomerName)
     {
         Value = i.CustomerCode
     });
 
ddlKlien.Items.AddRange(c);

And because you are adding items, you don't need the display member there, you are setting the display text when you are creating the RadListDataItem.

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

Best Regards,
Emanuel Varga
0
Robert Stuczynski
Top achievements
Rank 1
answered on 29 Sep 2010, 10:10 AM

Thanks it loks fine but if I try run it I see error:

01.System.ArgumentException was unhandled
02. Message="An item with the same key has already been added."
03. Source="mscorlib"
04. StackTrace:
05. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
06. at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
07. at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
08. at System.Data.Linq.Mapping.UnmappedType.GetDataMember(MemberInfo mi)
09. at System.Data.Linq.SqlClient.QueryConverter.VisitMemberInit(MemberInitExpression init)
10. at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
11. at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
12. at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
13. at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
14. at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
15. at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
16. at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
17. at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
18. at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
19. at Telerik.WinControls.UI.RadListDataItemCollection.AddRange(IEnumerable`1 range)
20. at ZlecTransp001.fMain.Wypelnij() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\fMain.cs:line 232
21. at ZlecTransp001.fMain..ctor() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\fMain.cs:line 36
22. at ZlecTransp001.Program.Main() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\Program.cs:line 18
23. at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
24. at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
25. at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
26. at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
27. at System.Threading.ThreadHelper.ThreadStart()
28. InnerException:
 
Do you know how resolve it?

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 10:23 AM
Hello Robert,

You are adding them somewhere in a dictionary?
I know for sure that you can have two or more items with the same Text and Value in a DropDownList, so this is not the problem, sadly without seeing more of your code i cannot tell you what else is going on, but please check if you are not doing something else on an event fired by the DropDownList that could cause this behavior.

To start with, you could unregistered all of the events and just run the application like that and see if this problem persists.

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

Best Regards,
Emanuel Varga
0
Robert Stuczynski
Top achievements
Rank 1
answered on 29 Sep 2010, 10:59 AM

Hi Emanuel

OK I cut all code in my program and object from form.

In this moment I had only one object dropdownlist ddlKlien

and this code:

01.using System;
02.using System.Drawing;
03.using System.Linq;
04.using System.Windows.Forms;
05.using Microsoft.Reporting.WinForms;
06.using Telerik.WinControls;
07.using Telerik.WinControls.UI;
08.  
09.namespace ZlecTransp001
10.{
11.   public partial class fMain : Form
12.   {
13.      private readonly bazyDanychDataContext dbS;
14.      private readonly zleceniaTransportoweDataContext dbZ;
15.  
16.      public fMain()
17.      {
18.         InitializeComponent();
19.         dbZ = new zleceniaTransportoweDataContext();
20.         dbS = new bazyDanychDataContext();
21.  
22.         Wypelnij();
23.      }
24.  
25.      public void Wypelnij()
26.      {
27.         ddlKlien.Items.Clear();
28.         var x = dbS.view_Trans_Order_Custs.OrderBy(o => o.CustomerCode).Select(
29.              o => new RadListDataItem(o.CompleteCustomerName)
30.              {
31.                 Value = o.CustomerCode
32.              });
33.  
34.         ddlKlien.Items.AddRange(x);
35.      }
36.   }
37.}

But all time I see this error when I run compile:

01.System.ArgumentException was unhandled
02.  Message="An item with the same key has already been added."
03.  Source="mscorlib"
04.  StackTrace:
05.       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
06.       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
07.       at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
08.       at System.Data.Linq.Mapping.UnmappedType.GetDataMember(MemberInfo mi)
09.       at System.Data.Linq.SqlClient.QueryConverter.VisitMemberInit(MemberInitExpression init)
10.       at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
11.       at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
12.       at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
13.       at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
14.       at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
15.       at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
16.       at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
17.       at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
18.       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
19.       at Telerik.WinControls.UI.RadListDataItemCollection.AddRange(IEnumerable`1 range)
20.       at ZlecTransp001.fMain.Wypelnij() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\fMain.cs:line 35
21.       at ZlecTransp001.fMain..ctor() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\fMain.cs:line 23
22.       at ZlecTransp001.Program.Main() in c:\Users\PLRoStu\Documents\Visual Studio 2008\Projects\100929 ZlecTrans Test od Telerik testy\ZlecTransp001\Program.cs:line 18
23.       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
24.       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
25.       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
26.       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
27.       at System.Threading.ThreadHelper.ThreadStart()
28.  InnerException: 

What do you think about it?

0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 01:12 PM
Hello again,

I've prepared a form with a collection that does the same thing, please try it and tell me what's different, except the data context

namespace TestDropDownList
{
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
    using System.Drawing;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(300, 60);
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            var radDropDownList = new RadDropDownList();
            radDropDownList.Dock = DockStyle.Top;
            this.Controls.Add(radDropDownList);
 
            var x = new TestCollection(100).OrderBy(o => o.Id).Select(o => new RadListDataItem(o.Name)
            {
                Value = o.Id
            });
            radDropDownList.Items.AddRange(x);
        }
    }
 
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public Test(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
 
    public class TestCollection : BindingList<Test>
    {
        public TestCollection(int no)
        {
            for (int i = 0; i < no; i++)
            {
                this.Add(new Test(i, "item" + i));
            }
        }
    }
}


Please let me know,

Best Regards,
Emanuel Varga
0
Peter
Telerik team
answered on 01 Oct 2010, 01:09 PM
Hi,

Emanuel's solution is correct. Please try to set the data source of RadDropDownList using Items.AddRange.

I hope this helps.

Regards,
Peter
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robert Stuczynski
Top achievements
Rank 1
answered on 01 Oct 2010, 01:12 PM

I try this and answer in next week - sorry.

Thansk for help

0
Robert Stuczynski
Top achievements
Rank 1
answered on 13 Oct 2010, 10:15 AM

I am sorry for big delay. And thanks again for your help

Today I take one hour for test it. Your code work correctly but my not :( I think there is little mistake but I do not know where. I am begginer in C# and maybe it is reason ;)

I thinks it is mistake in this query becasue x not return good value. Could you look on this code and on the picture?

http://www.fotosik.pl/pokaz_obrazek/pelny/06107702b089cea8.html 

0
Stefan
Telerik team
answered on 14 Oct 2010, 03:27 PM
Hello Robert,

Generally, you can find the answer to your question in the support ticket that you have opened. Still, I am posting the same answer here in case our community is interested in it.

After deep investigation of this issue, we came to the conclusion that this exception is coming from the entity framework and it has nothing to do with our controls suite. The following example, where I am creating a custom list, demonstrates where the exception comes from:
public partial class Form1 : Form
   {
      private readonly testDBDataContext dbS;
 
      public class customList : List<RadListDataItem>
      {
          public void AddRange(IEnumerable<RadListDataItem> range)
          {
              foreach (RadListDataItem item in range)
              {
                  this.Add(item);
              }
          }
      }
 
      public Form1()
      {
         InitializeComponent();
         dbS=new testDBDataContext();
 
         Wypelnij();
      }
 
      public void Wypelnij()
      {
          customList list = new customList();
          list.Clear();
          var x = dbS.view_Trans_Order_Custs.OrderBy(o => o.CustomerCode).Select(
               o => new RadListDataItem(o.CompleteCustomerName)
               {
                   Value = o.CustomerCode
               });
 
          list.AddRange(x);
 
      }

I hope you find this information helpful.
 
Sincerely yours,
Stefan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Sultan
Top achievements
Rank 1
answered on 10 Feb 2011, 05:46 AM

Hi Stefan,

 I have also tried to change Microsoft combobox to Telerik new drop down list.

For example, I have used this code to populate Microsoft combobox:
this.cbSalutation.Items.AddRange(new object[] {
            "Mr.",
            "Ms.",
            "Mrs.",
            "Mdm.",
            "Dr."});
How can i change this for Telerik drop down list.

0
Stefan
Telerik team
answered on 11 Feb 2011, 05:13 PM
Hello Sultan,

Thank you for writing.

Since RadDropDownList is using RadListDataItems for its Item collection, you have to create an instance of RadListDataItem instead of object:
 
this.radDropDownList1.Items.AddRange(new RadListDataItem[]
{
new RadListDataItem("item 1"),
new RadListDataItem("item 2"),
new RadListDataItem("item 3"),
new RadListDataItem("item 4"),
new RadListDataItem("item 5")
});

I hope this helps.

Best wishes,
Stefan
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
Accepted
Qaiser
Top achievements
Rank 1
answered on 27 May 2012, 11:51 AM
            this.comboBoxValueLocation.Items.AddRange(new string[] {
            "Left of Key Text",
            "Right of Key Text",
            "Below Key Text",
            "Above Key Text"});

You can also use this option, simply replace object with string and it works.
Tags
DropDownList
Asked by
Robert Stuczynski
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Robert Stuczynski
Top achievements
Rank 1
Peter
Telerik team
Stefan
Telerik team
Sultan
Top achievements
Rank 1
Qaiser
Top achievements
Rank 1
Share this question
or