Page view counter

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

Last post 11-01-2007 1:16 PM by richiej. 4 replies.

Sort Posts:

  • Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

    11-01-2007, 12:25 PM
    • Loading...
    • richiej
    • Joined on 09-25-2006, 6:24 AM
    • Posts 339
    • Points 1,696

     Hi,

    I'll try to make this straightforward

     
    I have a gridview on my aspx page:

                            <asp:GridView ID="ObjectList" runat="server" AutoGenerateColumns="false">
                                <Columns>
                                    <asp:BoundField DataField="ObjID" Visible="false"/>
                                    <asp:BoundField DataField="EnglishEqu"  HeaderText='<%EngTitle%>' />
                                    <asp:TemplateField HeaderText='<%NewTitle%>'>
                                        <ItemTemplate>
                                            <asp:TextBox ID="NewLangEqu" runat="server" MaxLength="255"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

     

    I need to bind this list to a collection which is built up in my code behind page:

        code behind:

            int iVal = int.Parse(ListLanguageList.SelectedValue);
            DataSet ds = DatabaseMethods.GetLanguageObjectSet(iVal);

            LanguageGridCollection col = new LanguageGridCollection();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                LanguageGridClass lan = new LanguageGridClass();
                lan.ObjID = dr[0].ToString();
                lan.NewLabel = dr[1].ToString();

                //loop through each property in the main resource file
                foreach (PropertyInfo info in (typeof(Resources.ClaimTracking)).GetProperties())
                {
                    //if the property is a string then....
                    if (info.PropertyType == typeof(string))
                    {
                        if (info.Name == lan.ObjID)
                        {
                            lan.EnglishLabel = info.GetValue(info, null).ToString();
                        }
                        col.Add(lan);
                    }
                }
            }

            //we now have a list of objIDs and english labels and new labels (if a new label exists)
            ObjectList.DataSource = col;
            ObjectList.DataBind();

     

     

    custom class and collection codecollection code 

     

     

    public class LanguageGridClass
    {
        public LanguageGridClass() { }

        //the names of the items in the RESX files are unique, ergo each ObjID will be unique
        private string objID;
        public string ObjID
        {
            get
            {
                return objID;
            }
            set
            {
                objID = value;
            }
        }

        private string englishLabel;
        public string EnglishLabel
        {
            get
            {
                return englishLabel;
            }
            set
            {
                englishLabel = value;
            }
        }

        private string newLabel;
        public string NewLabel
        {
            get
            {
                return newLabel;
            }
            set
            {
                newLabel = value;
            }
        }
    }

    public class LanguageGridCollection : IColumns<LanguageGridClass>
    {
        //member variable - used to store the actual list
        private List<LanguageGridClass> Items = new List<LanguageGridClass>();
           
        //override, return the list
        public List<LanguageGridClass> ListOfColumns()
        {
            return Items;
        }

        //add to the list
        public void Add(LanguageGridClass item)
        {
            Items.Add(item);
        }

        //remove from the list
        public void Remove(LanguageGridClass item)
        {
            Items.Remove(item);
        }

        //clear all columns from list
        public void Clear()
        {
            Items.Clear();
        }

        public LanguageGridClass this[int index]
        {
            get
            {
                return ((LanguageGridClass)(this.Items[index]));
            }
            set
            {
                this.Items[index] = value;
            }
        }
    }

     

     The error I get is the one in the subject line and it occurs on "ObjectList.DataSource = col;" line

     

    I am not really sure I fully understand the error nor do I know how to go about fixing this.

     

    Anyone with any ideas???

     

    many thanks
     

  • Re: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

    11-01-2007, 12:54 PM

    I believe that your class needs to implement the IDataSource interface (or, IListSource or IEnumerable interfaces) in order for it to be valid to set it as the DataSource property of a GridView.

    I would work on making your class implement one of those interfaces. 

  • Re: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

    11-01-2007, 1:01 PM
    • Loading...
    • hardikpatel
    • Joined on 08-30-2007, 1:27 PM
    • Posts 206
    • Points 1,053

    Hi

    LanguageGridCollection Class should be implements iCollection Interface so defination should be

     

    public class LanguageGridCollection 

                          implements ICollection
     

    - Hardik (Software Developer,Surat,India)

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

    11-01-2007, 1:08 PM
    • Loading...
    • richiej
    • Joined on 09-25-2006, 6:24 AM
    • Posts 339
    • Points 1,696

    thanks for the reply

    I kinda guess that from the error messgae though. But which interface should I try to implement, (which one is better/more suited to my needs)?

     

    thanks 


  • Re: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

    11-01-2007, 1:16 PM
    Answer
    • Loading...
    • richiej
    • Joined on 09-25-2006, 6:24 AM
    • Posts 339
    • Points 1,696

    Ahh. I think I may have cracked it. The reply about ICollection got me thinking..... and I by inheriting from CollectionBase my error is solved!!

     Thanks for the help
     

Page 1 of 1 (5 items)