Пример #1
0
        public Form1()
        {
            InitializeComponent();
            FilteredBindingList <Employee> employees = new FilteredBindingList <Employee>();

            employees.Add(new Employee("Aaberg", "Jesper", 52000, new DateTime(2004, 1, 14)));
            employees.Add(new Employee("Haas", "Jonathan", 75000, new DateTime(2003, 2, 14)));
            employees.Add(new Employee("Adams", "Ellen", 52000, new DateTime(2006, 10, 16)));
            employees.Add(new Employee("Hanif", "Kerim", 60000, new DateTime(2005, 10, 4)));
            employees.Add(new Employee("Akers", "Kim", 55000, new DateTime(2004, 1, 14)));
            employees.Add(new Employee("Harris", "Phyllis", 66000, new DateTime(2007, 3, 10)));
            employees.Add(new Employee("Andersen", "Elizabeth", 55000, new DateTime(2003, 7, 10)));
            employees.Add(new Employee("Alberts", "Amy", 40000, new DateTime(2005, 7, 7)));
            employees.Add(new Employee("Hamlin", "Jay", 45000, new DateTime(2005, 8, 8)));
            employees.Add(new Employee("Hee", "Gordon", 35000, new DateTime(2004, 9, 9)));
            employees.Add(new Employee("Penor", "Lori", 52000, new DateTime(2004, 11, 11)));
            employees.Add(new Employee("Pfeiffer", "Michael", 51000, new DateTime(2006, 12, 12)));
            bindingSource1.DataSource            = employees;
            dataGridView1.DataSource             = bindingSource1;
            dataGridView1.BindingContextChanged += new EventHandler(dataGridView1_BindingContextChanged);

            showAllLabel.Text         = "Show &All";
            showAllLabel.Visible      = false;
            showAllLabel.Click       += new EventHandler(showAllLabel_Click);
            showAllLabel.IsLink       = true;
            showAllLabel.LinkBehavior = LinkBehavior.HoverUnderline;

            dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
        }
Пример #2
0
        /// <summary>
        /// Откртие файла
        /// </summary>
        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
                try
                {
                    using (var stream = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite))
                    {
                        using (var reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            bl.Clear();
                            do
                            {
                                bool first = true;
                                while (reader.Read() && !IsEmptyRow(reader))
                                {
                                    if (first)
                                    {
                                        int i = 0;
                                        foreach (DataGridViewColumn col in dataGridView1.Columns)
                                        {
                                            if (col.HeaderText == "Index")
                                            {
                                                continue;
                                            }

                                            if (col.HeaderText != "Index" && reader.GetString(i) != col.HeaderText)
                                            {
                                                MessageBox.Show(
                                                    "Данный файл нельзя открыть. Откройте таблицу про гостиницы.");
                                                return;
                                            }
                                            i++;
                                        }

                                        first = false;
                                        continue;
                                    }

                                    for (int i = 0; i < reader.ResultsCount; i++)
                                    {
                                        bl.Add(new Hotel(new Address(reader.GetString(5)),
                                                         new Address(reader.GetString(6)),
                                                         reader.GetString(0), reader.GetString(1),
                                                         reader.GetString(7), reader.GetString(2), reader.GetString(3),
                                                         reader.GetString(4), reader.GetString(11), reader.GetString(12),
                                                         reader.GetString(13), reader.GetString(14), reader.GetString(15),
                                                         reader.GetString(16),
                                                         reader.GetString(8), reader.GetString(9), reader.GetString(10),
                                                         reader.GetString(17)));
                                    }
                                }
                            } while (reader.NextResult());
                            button1.Visible = true;
                            button2.Visible = true;
                        }
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (HeaderException ex)
                {
                    MessageBox.Show("Данный файл невозможно открыть. Вероятно, он поврежден.");
                }
            }
            else
            {
                return;
            }

            BindingSource bs = new BindingSource();

            bs.DataSource            = bl;
            dataGridView1.DataSource = bs;
        }