Import Data from Excel to DataGridView in C# Archives - Tech Insights https://reactconf.org/category/import-data-from-excel-to-datagridview-in-c/ Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Thu, 21 Mar 2013 07:23:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 Import Data from Excel to DataGridView in C# Archives - Tech Insights https://reactconf.org/category/import-data-from-excel-to-datagridview-in-c/ 32 32 230003556 Import Data from Excel to DataGridView in C# https://reactconf.org/importexceldataintodatagridusingcsharp/ https://reactconf.org/importexceldataintodatagridusingcsharp/#comments Thu, 21 Mar 2013 07:23:00 +0000 http://www.sqlneed.com/2013/03/21/import-data-from-excel-to-datagridview-in-c/ First need to add the reference “Microsoft.Office.Interop.Excel”. You can add it from .Net components. Add an open Dialog box control, Datagridview1, Textbox control and two Command buttons on form using …

The post Import Data from Excel to DataGridView in C# appeared first on Tech Insights.

]]>

First need to add the reference “Microsoft.Office.Interop.Excel”. You can add it from .Net components.
Add an open Dialog box control, Datagridview1, Textbox control and two Command buttons on form
using Excel = Microsoft.Office.Interop.Excel;
                                                                       
private voidbutton1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
                textBox1.Text=openFileDialog1.FileName;
        }
private voidbutton2_Click(object sender, EventArgs e)
{
     try
            {
                System.Data.OleDb.OleDbConnectionMyCnn ;
                System.Data.DataSet DSet ;
                System.Data.OleDb.OleDbDataAdapterMyCmd ;
MyCnn = new System.Data.OleDb.OleDbConnection(“provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + textBox1.Text + ” ;Extended Properties=Excel 8.0;”);
                MyCmd = new System.Data.OleDb.OleDbDataAdapter 
                (“select * from [Sheet1$]”, MyCnn);
                MyCmd.TableMappings.Add(“Table”, “TestTable”);
                DSet = new System.Data.DataSet();
                MyCmd.Fill(DSet);
                dataGridView1.DataSource = DSet.Tables[0];
                MyCnn.Close();
            }
            catch (Exceptionex)
            {
                MessageBox.Show (ex.ToString());
            }
 }

Output :

The post Import Data from Excel to DataGridView in C# appeared first on Tech Insights.

]]>
https://reactconf.org/importexceldataintodatagridusingcsharp/feed/ 2 2228