insert data from excel file into datagrid
-
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); }
-
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); }
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
-
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); }
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.
-
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.
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
-
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
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
-
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
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
-
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
I test The same code works with wf but it does not work with wpf datagrid