WinUI | ComponentOne
In This Topic
    Data Source
    In This Topic

    This topic covers the DataSource used for the QuickStart topic, that is, the Dataprovider class.

    internal class DataProvider
    {
        public static IEnumerable<Car> GetCarDataCollection(DataTable carTable)
        {
            Random rnd = new Random();
            foreach (DataRow row in carTable.Rows)
            {
                yield return new Car
                {
                    ID = row.Field<int>("ID"),
                    Brand = row.Field<string>("Brand"),
                    Model = row.Field<string>("Model"),
                    HP = row.Field<Int16>("HP"),
                    Liter = row.Field<double>("Liter"),
                    Cyl = row.Field<Int16>("Cyl"),
                    TransmissSpeedCount = row.Field<Int16>("TransmissSpeedCount").ToString(),
                    TransmissAutomatic = row.Field<string>("TransmissAutomatic"),
                    MPG_City = row.Field<Int16>("MPG_City"),
                    MPG_Highway = row.Field<Int16>("MPG_Highway"),
                    Category = row.Field<string>("Category"),
                    Description = row.Field<string>("Description"),
                    Hyperlink = row.Field<string>("Hyperlink"),
                    Picture = row.Field<byte[]>("Picture"),
                    Price = row.Field<double>("Price")
                };
            }
        }
    
        public static DataTable GetCarTable()
        {
            DataTable dt = new DataTable("Cars");
            var asm = Assembly.GetExecutingAssembly();
            using (var s = asm.GetManifestResourceStream("QuickStartDemo.Resources.cars.xml"))
            {
                _ = dt.ReadXml(s);
            }
            return dt;
        }
    
        private static readonly Random s_rnd = new Random();
    
        public static string[] Colors { get; } = new string[] { "Red", "Green", "Blue", "Black", "Silver", "Gold", "Gray" };
    
        
        private static List<int> GetUniqueRandomIndexes(int count, int max, int min = 0)
        {
            var indexes = new List<int>();
            if (count < max - min)
            {
                while (count > 0)
                {
                    var index = s_rnd.Next(min, max);
                    if (!indexes.Contains(index))
                    {
                        indexes.Add(index);
                        count--;
                    }
                }
            }
            return indexes;
        }
    
        public static IEnumerable<Car> GetCars()
        {
            var carsTable = GetCarTable();
            foreach (DataRow row in carsTable.Rows)
            {
                yield return new Car
                {
                    Brand = row.Field<string>("Brand"),
                    Category = row.Field<string>("Category"),
                    Description = row.Field<string>("Description"),
                    Liter = row.Field<double>("Liter"),
                    Model = row.Field<string>("Model"),
                    Picture = row.Field<byte[]>("Picture"),
                    Price = row.Field<double>("Price"),
                    TransmissAutomatic = row.Field<string>("TransmissAutomatic"),
                    TransmissSpeedCount = row.Field<Int16>("TransmissSpeedCount").ToString(),
    
                    ID = row.Field<int>("ID")
                };
            }
        }
    }
    
    internal class TransmissAutomatic
    {
        public string DisplayValue { get; set; }
        public object Value { get; set; }
    }