Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. C# How to populate datagridview from a separate combobox selection

C# How to populate datagridview from a separate combobox selection

Scheduled Pinned Locked Moved Design and Architecture
csharpvisual-studiolinqgraphics
4 Posts 4 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mourad barsoum
    wrote on last edited by
    #1

    I am familiar with Excel VBA but new to C# and Visual Basicvisula Studio. I have an Excel file (.xlsm) with several sheets. Using ExcelDataReader, I managed to build a solution to show all sheet names in a combobox control. I need help in how can I import the selected sheet content in combobox (cboSheet) to the datagridview1

    using ExcelDataReader;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;

    namespace ReadandImportsheet1
    {
    public partial class Form1 : Form
    {
        
        
        private readonly object con;
        
    
        public object Break { get; private set; }
    
        public Form1()
        {
            InitializeComponent();
        }
    
        public Form1(object con)
        {
            this.con = con;
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            OpenFileDialog fil = new OpenFileDialog();
            fil.ShowDialog();
            string path = fil.FileName.ToString();
            ExcelFileReader(path);
        }
        public void ExcelFileReader(string path)
        {
            var stream = File.Open(path, FileMode.Open, FileAccess.Read);
            var reader = ExcelReaderFactory.CreateReader(stream);
            var result = reader.AsDataSet();
            var tables = result.Tables;
            foreach (DataTable table in tables)
            {
                cboSheet.Items.Add(table.TableName);
            }
            //var shname = cboSheet.SelectedValue;
            //string table\_name = cboSheet.Text;
        }
    
        private void cboSheet\_SelectedIndexChanged(object sender, EventArgs e)
        {
            string table\_name = cboSheet.Text;
            SqlConnection Con = new SqlConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = path; Extended Properties = "Excel 8.0; HDR = YES";");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select \* from " + table\_name + " ", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables\[0\];
            dataGridView1.DataBind();
    
            con.Close();
        }
    }
    

    }

    I am using Win 11 and Visual Studio 2019 with ExcelDataReade

    L V 2 Replies Last reply
    0
    • M mourad barsoum

      I am familiar with Excel VBA but new to C# and Visual Basicvisula Studio. I have an Excel file (.xlsm) with several sheets. Using ExcelDataReader, I managed to build a solution to show all sheet names in a combobox control. I need help in how can I import the selected sheet content in combobox (cboSheet) to the datagridview1

      using ExcelDataReader;
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.IO;
      using System.Data.SqlClient;

      namespace ReadandImportsheet1
      {
      public partial class Form1 : Form
      {
          
          
          private readonly object con;
          
      
          public object Break { get; private set; }
      
          public Form1()
          {
              InitializeComponent();
          }
      
          public Form1(object con)
          {
              this.con = con;
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              OpenFileDialog fil = new OpenFileDialog();
              fil.ShowDialog();
              string path = fil.FileName.ToString();
              ExcelFileReader(path);
          }
          public void ExcelFileReader(string path)
          {
              var stream = File.Open(path, FileMode.Open, FileAccess.Read);
              var reader = ExcelReaderFactory.CreateReader(stream);
              var result = reader.AsDataSet();
              var tables = result.Tables;
              foreach (DataTable table in tables)
              {
                  cboSheet.Items.Add(table.TableName);
              }
              //var shname = cboSheet.SelectedValue;
              //string table\_name = cboSheet.Text;
          }
      
          private void cboSheet\_SelectedIndexChanged(object sender, EventArgs e)
          {
              string table\_name = cboSheet.Text;
              SqlConnection Con = new SqlConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = path; Extended Properties = "Excel 8.0; HDR = YES";");
              con.Open();
              SqlDataAdapter da = new SqlDataAdapter("select \* from " + table\_name + " ", con);
              DataSet ds = new DataSet();
              da.Fill(ds);
              dataGridView1.DataSource = ds.Tables\[0\];
              dataGridView1.DataBind();
      
              con.Close();
          }
      }
      

      }

      I am using Win 11 and Visual Studio 2019 with ExcelDataReade

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Export the Excel as CSV. Then you can see what you're getting the "easy" way.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      • M mourad barsoum

        I am familiar with Excel VBA but new to C# and Visual Basicvisula Studio. I have an Excel file (.xlsm) with several sheets. Using ExcelDataReader, I managed to build a solution to show all sheet names in a combobox control. I need help in how can I import the selected sheet content in combobox (cboSheet) to the datagridview1

        using ExcelDataReader;
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        using System.IO;
        using System.Data.SqlClient;

        namespace ReadandImportsheet1
        {
        public partial class Form1 : Form
        {
            
            
            private readonly object con;
            
        
            public object Break { get; private set; }
        
            public Form1()
            {
                InitializeComponent();
            }
        
            public Form1(object con)
            {
                this.con = con;
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
                OpenFileDialog fil = new OpenFileDialog();
                fil.ShowDialog();
                string path = fil.FileName.ToString();
                ExcelFileReader(path);
            }
            public void ExcelFileReader(string path)
            {
                var stream = File.Open(path, FileMode.Open, FileAccess.Read);
                var reader = ExcelReaderFactory.CreateReader(stream);
                var result = reader.AsDataSet();
                var tables = result.Tables;
                foreach (DataTable table in tables)
                {
                    cboSheet.Items.Add(table.TableName);
                }
                //var shname = cboSheet.SelectedValue;
                //string table\_name = cboSheet.Text;
            }
        
            private void cboSheet\_SelectedIndexChanged(object sender, EventArgs e)
            {
                string table\_name = cboSheet.Text;
                SqlConnection Con = new SqlConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = path; Extended Properties = "Excel 8.0; HDR = YES";");
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select \* from " + table\_name + " ", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables\[0\];
                dataGridView1.DataBind();
        
                con.Close();
            }
        }
        

        }

        I am using Win 11 and Visual Studio 2019 with ExcelDataReade

        V Offline
        V Offline
        Vrend Top
        wrote on last edited by
        #3

        I am familiar with Excel VBA but new to C# and Visual Basicvisula Studio.

        R 1 Reply Last reply
        0
        • V Vrend Top

          I am familiar with Excel VBA but new to C# and Visual Basicvisula Studio.

          R Offline
          R Offline
          RedDk
          wrote on last edited by
          #4

          Well aren't you just the most delightful ... :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups