Re: Generics SortedList and DataSource

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



SHEBERT,

It's exactly what the error says. The DataSource needs to implement
IList or IListSource. The SortedList<TKey, TValue> class does not implement
IList or IListSource interface, so it can't be used for the DataSource
property on the ComboBox.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"SHEBERT" <SHEBERT@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:912CBC27-1186-4953-BF6B-98A296D5C7A2@xxxxxxxxxxxxxxxx
by the way, the error is

"Complex DataBinding accepts as a data source either an IList or an
IListSource."



"SHEBERT" wrote:

Here is an example of a SortedList that works as a datasource to the
ComboBox
and a generic SortedList<> that does not works as a datasource to the
ComboBox.

Why? If I use List and generic List<>, both works.

private void Form1_Load(object sender, EventArgs e)
{
System.Collections.SortedList QA1 = new
System.Collections.SortedList();
QA1["Name1"] = new Query("Name1", "Group1", true);
QA1["Name2"] = new Query("Name2", "Group1", true);
comboBox1.DataSource = QA1.Values; // Works !


System.Collections.Generic.SortedList<string, Query> QA2 =
new
System.Collections.Generic.SortedList<string, Query>();
QA2["Name1"] = new Query("Name1", "Group1", true);
QA2["Name2"] = new Query("Name2", "Group1", true);
comboBox1.DataSource = QA2.Values; // Does not works
}



Here's the QUERY.cls code in case you're wondering:

sing System;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Query.
/// </summary>
public class Query : IComparable
{
string m_name ;
string m_group;
string m_email;
bool m_IsDirect;


public Query(string Name , string Group, bool IsDirect )
{
m_name = Name;
m_group = Group;
m_IsDirect = IsDirect;
m_email = "";
}

public Query(string Name , string Group, bool IsDirect, string Email)
{
m_name = Name;
m_group = Group;
m_email = Email;
m_IsDirect = IsDirect;
}

public string Email
{
get
{
return m_email;
}
set
{
m_email = value;
}
}

public bool IsDirect
{
get
{
return m_IsDirect;
}
set
{
m_IsDirect = value;
}
}

public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}

public string Group
{
get
{
return m_group;
}
set
{
m_group = value;
}
}

public string Display
{
get
{
return string.Format("{0}{1}\t{2}",m_IsDirect ? " \t" : "\x2206\t",
m_group, m_name);
}
}
public string DisplayALL
{
get
{
return string.Format("{0}\t{1}\t{2}",m_group, m_name, m_email);
}
}

#region IComparable Members
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
// int IComparer.Compare( Object one, Object two ) {
// return( (new CaseInsensitiveComparer()).Compare( y, x ) );
// }

public int CompareTo(object obj)
{
if(obj is Query)
{
Query item = (Query) obj;
return m_name.CompareTo(item.m_name);
}
throw new ArgumentException("This object is not a Query Class");
}

#endregion
}
}



.



Relevant Pages

  • Re: Generics SortedList and DataSource
    ... /// Summary description for Query. ... string m_group; ... public Query(string Name, string Group, bool IsDirect) ... public string Email ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generics SortedList and DataSource
    ... DataSource) and not generics SortedList? ... /// Summary description for Query. ... string m_group; ... public string Email ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Generics SortedList and DataSource
    ... /// Summary description for Query. ... string m_group; ... public Query(string Name, string Group, bool IsDirect) ...
    (microsoft.public.dotnet.languages.csharp)
  • Generics SortedList and DataSource
    ... /// Summary description for Query. ... string m_group; ... public Query(string Name, string Group, bool IsDirect) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)