T
T
tmtm6662016-10-12 20:59:43
.NET
tmtm666, 2016-10-12 20:59:43

How to extract data from excel to datagridview by two conditions?

AVAILABLE
Database in Excel
Database line - 10 000 pcs;
columns - 10 pcs;
QUESTION
1. What is the best way to extract data from Excel for two conditions in three different datagridviews?
2. The following columns of the table are retrieved:
- ID
- name
- count 
3. The table is retrieved with column headers (if possible);
THE CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection connection = new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=NO\";", textBox1.Text));

            connection.Open();
            OleDbCommand command = new OleDbCommand("SELECT * FROM [Лист1$]", connection);
            OleDbDataReader reader = command.ExecuteReader();
            dataGridView1.Columns.Add("1", "1");
            dataGridView2.Columns.Add("1", "1");
            dataGridView3.Columns.Add("1", "1");
            while (reader.Read())
            {
                if (reader[0].ToString() == textBox2.Text)
                {
                    if (Convert.ToInt32(reader[1]) > 10 && Convert.ToInt32(reader[1]) < 14) dataGridView1.Rows.Add(reader[1].ToString());
                    if (Convert.ToInt32(reader[1]) > 20 && Convert.ToInt32(reader[1]) < 24) dataGridView2.Rows.Add(reader[1].ToString());
                    if (Convert.ToInt32(reader[1]) > 30 && Convert.ToInt32(reader[1]) < 34) dataGridView3.Rows.Add(reader[1].ToString());
                }
            }
            connection.Dispose();
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Excel 2003(*.xls)|*.xls|Excel 2007(*.xlsx)|*.xlsx";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
    }
}

SCENARIO
1. Enter the job ID.
2. Enter the path to the file.
3. Press the "EXTRACT" button.
4. The data is retrieved depending on the "ID" and "sequence number" into the corresponding datagridview.
APPENDIX
1. Project.
http://file sharing.rf/w52ue994qlc1.html
2. Screen
9a04db75f16e4618915a147249f2b6ea.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2016-10-13
@dmitryKovalskiy

Do it humanly, not at the level of a student studying the platform on the second day. We write DAL in which there is a class for extracting data from an Excel file. We write a model class with the necessary fields. At the DAL level, we form a collection of model objects. At the presentation level (your application), we display parts of this collection in the DataGrid you need. No need to write data extraction with instant insertion in code-behind without intermediaries. Nobody does that. And if you show this at the interview, I will tell you the result right here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question