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

  • SEARCH
  • Home
  • 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 6579301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:53+00:00 2026-05-25T15:52:53+00:00

Hello well i try to import a Excel document into my DataGridView in C#.

  • 0

Hello well i try to import a Excel document into my DataGridView in C#.
So far it worked but there is a Column with Data in it i need to ‘sort’.

If this was simple i would do “WHERE test > 0” in the OleDbDataAdapter query.

But.. The name of the Column is changing with each document and i need to use it often. So far i got this :

    private void button1_Click(object sender, EventArgs e)
    {

        String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=C:\\Users\\Test\\Desktop\\Test.xls;" +
        "Extended Properties=Excel 8.0;";

        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter
        ("SELECT * FROM [Test$]", strConn);

        da.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0].DefaultView;

    }

In the select i need to put a line witch state that the first 3 letters of the column is the same but the number that follow are not. Like:

QTA 12345,
QTA 13213,
QTA 92818.

Something like:

    OleDbDataAdapter da = new OleDbDataAdapter
        ("SELECT * FROM [Test$] WHERE [testColumn] > 0", strConn);

But then with the same first 3 letters and the last numbers who are random.

Can someone help me please?

  • 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-25T15:52:53+00:00Added an answer on May 25, 2026 at 3:52 pm

    I’ve tried some code and it works fine for me. Have a try:

    OleDbConnection oleDbConnection = new OleDbConnection(
      "Provider=Microsoft.Jet.OLEDB.4.0;" +
      "Data Source=D:\\Users\\name\\Desktop\\test.xls;" +
      "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
    oleDbConnection.Open();
    
    //Get columns
    DataTable dtColumns = oleDbConnection.GetSchema("Columns", new string[] { null, null, "Tabelle1$", null });
    List<string> columns = new List<string>();
    
    foreach (DataRow dr in dtColumns.Rows)
      columns.Add(dr[3].ToString());
    
    string colName = columns.Find(item => item.Substring(0,3) == "QTA");
    
    DataSet ds = new DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter
      ("SELECT * FROM [Tabelle1$] WHERE [" + colName + "] > 0", oleDbConnection);
    da.Fill(ds);
    dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
    oleDbConnection.Close();
    

    Pay attention to changing the connection string to your needs.

    You can trim the code, using LINQ:

    string colName = (from DataRow dr in dtColumns.Rows where dr[3].ToString().Substring(0, 3) == "QTA" select dr[3].ToString()).ElementAt(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

file1.txt hello tom well file2.txt world jerry done How to merge file1.txt with file2.txt;
Hello I am compiling a program with make but I get the error of
Hello there and Merry Christmas !!! I am new to WPF and I am
So i've just finished the google app engine greetings tutorial. All well so far.
Is there any well known established framework for C or Java or PHP to
Well as the title states, anytime I try (in my java class) to settext
Hello there!. I am trying to use a public api from wunderground (more info
I am working on an application, with following XML. but whenI try to clean/build
When I try to build a simple Hello World C++ app on Windows 7
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc

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.