Esempio n. 1
0
        public Page()
        {
            InitializeComponent();

            var table = new DataTable();
            table.Columns.Add(new DataColumn() { ColumnName = "ID", DataType = typeof(int) });
            table.Columns.Add(new DataColumn() { ColumnName = "Name", DataType = typeof(string) });
            table.Columns.Add(new DataColumn() { ColumnName = "UnitPrice", DataType = typeof(decimal) });
            table.Columns.Add(new DataColumn() { ColumnName = "Date", DataType = typeof(DateTime) });
            table.Columns.Add(new DataColumn() { ColumnName = "Discontinued", DataType = typeof(bool) });

            for (int i = 0; i < 5; i++)
            {
                var row = table.NewRow();
                row["ID"] = i;
                row["Name"] = names[rnd.Next(9)];
                row["UnitPrice"] = prizes[rnd.Next(9)];
                row["Date"] = DateTime.Now.AddDays(i);
                row["Discontinued"] = bools[rnd.Next(9)];
                table.Rows.Add(row);
            }

            DataContext = table;
        }
Esempio n. 2
0
 protected internal DataRow(DataTable owner)
 {
     this.owner = owner;
 }
Esempio n. 3
0
        //TODO: Wait on http://www.telerik.com/community/forums/silverlight/gridview/lightweight-datatable-add-column-after-rows-have-been-added.aspx
        ///// <summary>
        ///// Adds a column to the DataTable.
        ///// </summary>
        ///// <param name="newColumnUniqueName">New name of the column unique.</param>
        //public ImportColumn AddColumn(string newColumnUniqueName)
        //{
        //    //Add the new column at the end
        //    var newColumn = new ImportColumn { ColumnName = newColumnUniqueName };
        //    DataTable.Columns.Add(newColumn);

        //    return newColumn;
        //}

        private static DataTable ReadInCSVData(CsvReader reader)
        {
            var dataTable = new DataTable();

            try
            {
                var headerRecord = reader.ReadHeaderRecord();
                foreach (var headerName in headerRecord.Values)
                    dataTable.Columns.Add(new ImportColumn { ColumnName = headerName, DataType = typeof(string) });

                //Add 3 extra columns
                for (var i = 0; i < 3; i++)
                    dataTable.Columns.Add(new ImportColumn { ColumnName = "Extra Column " + i, DataType = typeof(string) });

                foreach (var record in reader.DataRecords)
                {
                    var newRow = dataTable.NewRow();
                    var headerIndex = 0;
                    foreach (var headerKey in headerRecord.Values)
                    {
                        var cleanedString = record.Values[headerIndex].Replace('�', ' ');
                        newRow[headerKey] = cleanedString;
                        headerIndex++;
                    }

                    dataTable.Rows.Add(newRow);
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("The data needs to be in CSV format.", "Error importing data.", MessageBoxButton.OK);
                return null;
            }

            return dataTable;
        }