如何使用PagedDataSource CopyTo()?

时间:2012-10-18 11:02:51

标签: c# asp.net

调用ReadInDataFromRssAndReturnArrayWithTheDataStatic时出现错误,因为DataSource无法存储在CopyTo方法中指定的数组中。

我想知道我应该如何找到我应该使用的数组类型。 以下是我的一些代码:

   public static class StillingslisteStatic
    {
        public static string RssSourceStatic { get; set; }
        public static string StillingssideStatic { get; set; }
        //This contains all of the data from the rss-file!!
        public static PagedDataSource PgdsPositionsStatic { get; set; }

        //This function is written by Anders Branderud
        public static DataView[] ReadInDataFromRssAndReturnArrayWithTheDataStatic(PageData ledigeStillingerPage)
        {
            DataView[] myObjArray = null; //I also tried with DataTable

            if (ledigeStillingerPage["Stillingsside"] != null)
                StillingssideStatic = ledigeStillingerPage["Stillingsside"].ToString();
            if (ledigeStillingerPage["RssSource"] != null) RssSourceStatic = (string)ledigeStillingerPage["RssSource"];



            if (!string.IsNullOrEmpty(RssSourceStatic))
            {
                FilterTableStatic(LoadFeedStatic(RssSourceStatic, ledigeStillingerPage), 100); //This initializes PgdsPositionsStatic
                myObjArray = new DataView[PgdsPositionsStatic.Count]; //one DataTable  is one item
                PgdsPositionsStatic.CopyTo(myObjArray, 0);
            }

            return myObjArray;
        }

        private static DataTable LoadFeedStatic(string url, PageData ledigeStillingerPage)
        {

            var reader = new XmlTextReader(url);
            var ds = new DataSet();
            ds.ReadXml(reader);

            foreach (DataTable table in ds.Tables)
            {
                if (table.TableName == "item")
                {
                    return table;
                }
            }
            return null;
        }

        private static void FilterTableStatic(DataTable table2Filter, int count)
        {
            PgdsPositionsStatic = new PagedDataSource();
            if (table2Filter == null) return;

            int counter = 1;
            var deleteItems = new DataRow[table2Filter.Rows.Count];
            foreach (DataRow row in table2Filter.Rows)
            {
                if (counter > count)
                    deleteItems[counter - count] = row; ;
                counter++;
            }
            for (int i = 0; i < (counter - count); i++)
            {
                var row = (DataRow)deleteItems[i];
                if (row == null)
                    continue;
                table2Filter.Rows.Remove(row);
            }
            PgdsPositionsStatic.DataSource = table2Filter.DefaultView;
        }

错误讯息:

{"Message":"Object cannot be stored in an array of this type.","StackTrace":"   at System.Array.InternalSetValue(Void* target, Object value)\r\n   at System.Array.SetValue(Object value, Int32 index)\r\n   at System.Web.UI.WebControls.PagedDataSource.CopyTo(Array array, Int32 index)\r\n   at

0 个答案:

没有答案