@Glcn , Welcome to Microsoft Q&A, I noticed that you used a mix of two methods to display the data in excel in datagirdview. Actually, we only used one method to get what you wanted.
Here is a code example you could refer to.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text files| *.xlsx";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;MAXSCANROWS=0'";
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "Select * from [" + "Sheet1" + "$]";
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.