Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6936769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:16:36+00:00 2026-05-27T12:16:36+00:00

I am creating a Windows Forms Application using Visual Studio 2010. I populate data

  • 0

I am creating a Windows Forms Application using Visual Studio 2010.

I populate data to a DataGridView from an excel file using OleDbDataAdapter method.

Here is my code

dataGridView1.DataSource = null;
dataGridView1.Update();

var connectionString =
     string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";", fileName);


var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);

var ds = new DataSet();
DataTable t = new DataTable();

adapter.Fill(t);

dataGridView1.DataSource = t; 

Now Problem is if some cells are merged in the excel file output get bit different.
Here is the image for better understanding.
enter image description here

So how can I fix this problem ?

I think if I can identify the merge cells then I can fix this. But I don’t have a clear idea at the moment.

Is there a better way to represent excel data in grid view as it is in the excel file ?

Any answer would be help. Please share any suggestions.

Thanks

Yohan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:16:37+00:00Added an answer on May 27, 2026 at 12:16 pm

    My solution:

    protected void Page_Load(object sender, EventArgs e)
    {
        string path = @"C:\samples\firstexcel.xlsx";
        GetExcelSheetNames(path);
    }
    
    private void GetExcelSheetNames(string excelFile)
    {
        OleDbConnection objConn = null;
        System.Data.DataTable dt = null;
        try
        {
            DataSet ds = new DataSet();
            // Connection String. 
            String connString = @"Data Source=" + excelFile +            ";
            Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";
            // Create connection. 
            objConn = new OleDbConnection(connString);
            // Opens connection with the database. 
            objConn.Open();
            // Get the data table containing the schema guid, and also sheet names. 
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    
            if (dt == null)
            {
                return;
            }
    
            String[] excelSheets = new String[dt.Rows.Count];
            int i = 0;
            // Add the sheet name to the string array. 
            // And respective data will be put into dataset table 
    
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[i] = row["TABLE_name"].ToString();
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
                OleDbDataAdapter oleda = new OleDbDataAdapter();
                oleda.SelectCommand = cmd;
                oleda.Fill(ds, "TABLE");
                i++;
            }
    
            // Bind the data to the GridView 
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
            Session["Table"] = ds.Tables[0];
        }
    
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    
        finally
        {
            // Clean up. 
            if (objConn != null)
            {
                objConn.Close();
                objConn.Dispose();
            }
    
            if (dt != null)
            {
                dt.Dispose();
            }
        }
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
        GridView1.DataBind();
    }
    
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on creating a Windows Forms Application in C++ using Visual Studio
Im creating a c# application using visual studio 2010 on a windows 7 machine.
I am creating windows installer project using Visual Studio 2005. Is there an option
I'm developing an application for Windows Mobile Devices using Visual Studio .NET 2008 whose
I am working on a Windows Form-Based application in Visual Studio 2008 using C#
Ok So I'm creating a Windows Forms Application for some guy using C# and
Creating My Windows Form Application and using ADO.Net as Data Access layer and SQL
I'm creating a multilingual application using Windows Forms. I have created my multilingual content
I am currently using the Chart Control from System.Windows.Forms.DataVisualization to dynamically creating 10 chart
The basic setup is classic - you're creating a Windows Forms application that connects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.