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. WPF
  4. insert data from excel file into datagrid

insert data from excel file into datagrid

Scheduled Pinned Locked Moved WPF
csharpwpf
7 Posts 3 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.
  • M Offline
    M Offline
    MemberDotNetting
    wrote on last edited by
    #1

    hello,i'm developping a wpf application i want to add the result of parser class in a datagrid public class ExcelParser { public DataTable GetDataTableExcel(string datasource) { OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\""); theConnection.Open(); OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Feuil1$]", theConnection); DataSet theDS = new DataSet(); DataTable dt = new DataTable(); theDataAdapter.Fill(dt); return dt; } private void btnExcel_Click(object sender, RoutedEventArgs e) { ExcelParser exc = new ExcelParser(); dgvreceipient.ItemsSource=exc.GetDataTableExcel(datasource).DefaultView; dgvreceipient.DataContext = exc.GetDataTableExcel(datasource); }

    L B 2 Replies Last reply
    0
    • M MemberDotNetting

      hello,i'm developping a wpf application i want to add the result of parser class in a datagrid public class ExcelParser { public DataTable GetDataTableExcel(string datasource) { OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\""); theConnection.Open(); OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Feuil1$]", theConnection); DataSet theDS = new DataSet(); DataTable dt = new DataTable(); theDataAdapter.Fill(dt); return dt; } private void btnExcel_Click(object sender, RoutedEventArgs e) { ExcelParser exc = new ExcelParser(); dgvreceipient.ItemsSource=exc.GetDataTableExcel(datasource).DefaultView; dgvreceipient.DataContext = exc.GetDataTableExcel(datasource); }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      MemberDotNetting wrote:

      i want to add the result of parser class in a datagrid

      And what is your problem? BTW please edit the above and put <pre> tags around your code to make it more readable.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      M 1 Reply Last reply
      0
      • M MemberDotNetting

        hello,i'm developping a wpf application i want to add the result of parser class in a datagrid public class ExcelParser { public DataTable GetDataTableExcel(string datasource) { OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\""); theConnection.Open(); OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Feuil1$]", theConnection); DataSet theDS = new DataSet(); DataTable dt = new DataTable(); theDataAdapter.Fill(dt); return dt; } private void btnExcel_Click(object sender, RoutedEventArgs e) { ExcelParser exc = new ExcelParser(); dgvreceipient.ItemsSource=exc.GetDataTableExcel(datasource).DefaultView; dgvreceipient.DataContext = exc.GetDataTableExcel(datasource); }

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Whatever your problem is, I see some bad code:

        dgvreceipient.ItemsSource=exc.GetDataTableExcel(datasource).DefaultView;
        dgvreceipient.DataContext = exc.GetDataTableExcel(datasource);

        You call GetDataTableExcel() twice with the same parameter, and that call might take some time. Better do it once, assign the result to a local variable, and then use that. E.g.

        DataTable tmp=exc.GetDataTableExcel(datasource);
        dgvreceipient.ItemsSource=tmp.DefaultView;
        dgvreceipient.DataContext =tmp;

        Perhaps that will also solve your problem (some inconsistent behavior?), which you did not communicate to us.

        M 1 Reply Last reply
        0
        • B Bernhard Hiller

          Whatever your problem is, I see some bad code:

          dgvreceipient.ItemsSource=exc.GetDataTableExcel(datasource).DefaultView;
          dgvreceipient.DataContext = exc.GetDataTableExcel(datasource);

          You call GetDataTableExcel() twice with the same parameter, and that call might take some time. Better do it once, assign the result to a local variable, and then use that. E.g.

          DataTable tmp=exc.GetDataTableExcel(datasource);
          dgvreceipient.ItemsSource=tmp.DefaultView;
          dgvreceipient.DataContext =tmp;

          Perhaps that will also solve your problem (some inconsistent behavior?), which you did not communicate to us.

          M Offline
          M Offline
          MemberDotNetting
          wrote on last edited by
          #4

          the lines are empty, for example, I have 3 line in the excel file, I receive in the datagrid 3 line, but they are empty

          1 Reply Last reply
          0
          • L Lost User

            MemberDotNetting wrote:

            i want to add the result of parser class in a datagrid

            And what is your problem? BTW please edit the above and put <pre> tags around your code to make it more readable.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            M Offline
            M Offline
            MemberDotNetting
            wrote on last edited by
            #5

            the lines are empty, for example, I have 3 line in the excel file, I receive in the datagrid 3 line, but they are empty

            L 1 Reply Last reply
            0
            • M MemberDotNetting

              the lines are empty, for example, I have 3 line in the excel file, I receive in the datagrid 3 line, but they are empty

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              MemberDotNetting wrote:

              the lines are empty, for example, I have 3 line in the excel file, I receive in the datagrid 3 line, but they are empty

              Sorry, but that does not give us anywhere near enough information to help you. You need to set to work with your debugger to see what gets imported into your data objects.

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              M 1 Reply Last reply
              0
              • L Lost User

                MemberDotNetting wrote:

                the lines are empty, for example, I have 3 line in the excel file, I receive in the datagrid 3 line, but they are empty

                Sorry, but that does not give us anywhere near enough information to help you. You need to set to work with your debugger to see what gets imported into your data objects.

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                M Offline
                M Offline
                MemberDotNetting
                wrote on last edited by
                #7

                I test The same code works with wf but it does not work with wpf datagrid

                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