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. C#
  4. Wonder of DataGrid imort from Excel file

Wonder of DataGrid imort from Excel file

Scheduled Pinned Locked Moved C#
3 Posts 2 Posters 0 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.
  • L Offline
    L Offline
    Libra
    wrote on last edited by
    #1

    Has a Excel file like that:          Title Field1  Field2  Filed3 1        AA      1.1 2 3a      CC      3.3 4        DD      4.4 I wrote a program in order to import the excel file datas to DataGrid: OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Test.xls; Extended Properties=Excel 8.0"); connection.Open(); DataTable dt = new DataTable("Orders"); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = command; da.Fill(dt); this.dataGridTest.DataSource = dt; Why in the DataGrid show as: F1      Title   F2           ------> This is header (null)  Field2  Filed3 1       AA      1.1 2       (null)  (null) (null)  CC      3.3          ------> Note this line column 1 is null! 4       DD      4.4 If the Excel file like this:          Title Field1  Field2  Filed3 1       AA      1.1 2a 3a      CC      3.3 4a      DD      4.4 That the DataGrid turns to like this: F1      Title    F2           ------> This is header Field1  Field2  Filed3        ------> Note the Field1 now is not null (null)  AA      1.1            ------> Note this line column 1 is null! 2a      (null)  (null) 3a      CC      3.3 4a      DD

    K 1 Reply Last reply
    0
    • L Libra

      Has a Excel file like that:          Title Field1  Field2  Filed3 1        AA      1.1 2 3a      CC      3.3 4        DD      4.4 I wrote a program in order to import the excel file datas to DataGrid: OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Test.xls; Extended Properties=Excel 8.0"); connection.Open(); DataTable dt = new DataTable("Orders"); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = command; da.Fill(dt); this.dataGridTest.DataSource = dt; Why in the DataGrid show as: F1      Title   F2           ------> This is header (null)  Field2  Filed3 1       AA      1.1 2       (null)  (null) (null)  CC      3.3          ------> Note this line column 1 is null! 4       DD      4.4 If the Excel file like this:          Title Field1  Field2  Filed3 1       AA      1.1 2a 3a      CC      3.3 4a      DD      4.4 That the DataGrid turns to like this: F1      Title    F2           ------> This is header Field1  Field2  Filed3        ------> Note the Field1 now is not null (null)  AA      1.1            ------> Note this line column 1 is null! 2a      (null)  (null) 3a      CC      3.3 4a      DD

      K Offline
      K Offline
      Kodanda Pani
      wrote on last edited by
      #2

      Hi Libra, Can you try something with your command object. Insted of selecting a sheet can you try selecting a range of cells. command.CommandText = "Select * from [Sheet1$A2:B7]"; Thanks and Regards Pani.

      L 1 Reply Last reply
      0
      • K Kodanda Pani

        Hi Libra, Can you try something with your command object. Insted of selecting a sheet can you try selecting a range of cells. command.CommandText = "Select * from [Sheet1$A2:B7]"; Thanks and Regards Pani.

        L Offline
        L Offline
        Libra
        wrote on last edited by
        #3

        Thx for you help. I try as your said but it's trashy.:sigh:Select of a part datas is base on the Select of whole sheet. I wonder if the OleDb Engineer adjust the type of each excel column when transfer, so when the column looks has more numerical value that it make the numerical type, when the column looks has more string value that it make the string type.:~ === Game is power! ===

        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