C# wpf export to Excel issue - c#

I have a dataGrid in my WPF application. I can export this dataGrid due to this good tutorial :
http://www.codeproject.com/Articles/120480/Export-to-Excel-Functionality-in-WPF-DataGrid
My problem is : I get every column of my Object, and i'd want to display only the columns of my actual Datagrid, not every strings of my Object. One guy asked the question in this tutorial, he didn't get any answer.
Also, I want to switch the order of my columns, so I found this snappet :
Excel.Range copyRange = _range.Range["D:D"];
Excel.Range insertRange = _range.Range["A:A"];
insertRange.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, copyRange.Cut());
The only problem is : It only works one time, I can switch A column to D but once it's done I can't move B to E for example ...
So I heard about MyXls and other librairies (I'm quoting this one cause that's a free one and I can only use free librairies for this project).
I'm a bit lost, I've been working on this for 2 days and I don't know what to do.
Do you have a great librairy for Excel import in WPF ? Or do you have a snippet to switch columns and especially (most important) display only the DataGrid row ?
Thanks in advance !

Have you tried copying your list of objects into a smaller list of DTO objects that contain only the fields on the data grid? That way, the export to Excel functionality would only iterate the fields from the datagrid because that's all the datagrid knows about. I'd open a second question for the ordering / reordering of columns.

Related

How can I create a WPF data grid with an enhanced table header?

I'm currently working on a WPF based data management system, which is planned to replace the old Excel based one. This old system is mainly consist of tables where the user can input his data. One of these tables looks like this:
The number of rows and columns are fixed (25 rows per day, from 6AM to 6AM the next day).
It is kinda difficult to provide an input form for this kind of data, so I tried implementing the table in WPF using a DataGrid. However, I wasn't able to get these nifty merged cells, especially in the first and second row.
Are there any 'DataGrid' usercontrols on the market that allow merging cells, shading rows etc.? Or is there any other way of providing an user-friendly input form that I've overlooked?
If you're looking for Grid cell merging tool you can try Devexpress.
Here is a link on how to merge DataGrid cells using Devexpress
Another solution to merge DataGrid Rows is to use a ListBox

Dynamic Generation of Grid for MVC ASP.NET

I have posted similar question, but now I am re-posting it with more clear information about the problem I am dealing with.
I am transforming an existing application to a web based that is operating dynamically. It has grid consuming data from SQL table in such a way :
Columns can be added and deleted on runtime. (These columns are separated and shown as ListItem before showing the Grid on separate control, and user selects the columns for defining a Grid to have)
Column grouping can be done dynamically.
Custom settings can be defined for this Grid ( e.g
On runtime , when each row gets data completed, next row's few
mandatory columns get last row's those cell values automatically and
so on....)
and the whole Grid containing data for 10000's of objects must not disturb apart from the changes user can make.
I have searched a lot over the internet, I don't think so for complying all above requirements anz solution among ( qGrid, DevExpress or TelerikGrid ) would work. As DevExpress GridView can not be operated dynamically in this way.
What about SpreadSheet? Does this problem can be better solve able by DevExpress Spreadsheet control? In that case, still defining these configurations would be a problem? These configurations can be saved/altered for specific data objects ( Currently, these configurations are saved as seperate table and are being referenced for specific data set ).
What are the possible solutions to approach this scanario? ( SpreadSheet ? )
Regards
Usman

DataTable columns inside columns or merged row table structure

I've had a Google but I find no answer. You know how say in MS Word, when you insert a table, you can merge the columns of one single row to create a column-less row? Or have say a main header and then more columns inside it? Like so:
Merged Row:
|Column 1|Column 2|Column 3|
|Value|Value|Value|
|Merged Row|
|Value|Value|Value|
God the text formatting is awful on this web-site...and the Header Column example:
|Main Header||Another Main Header|
|Column 1|Column2||Column1|Column2|
I'm trying to achieve this in a written DataTable in C# which is being populated by an array. But I'm interested in changing its structure as one of the above, whichever is possible/easier.
So I've created my DataTable and added normal columns and rows etc. But I'd like to create either a merged row or a main header to display. Instead of having a repeated value in a single column for the values I am getting.
Can someone please provide a quick example on how I could achieve either one of those? Because repeated value will just look ugly.
Cheers.
You're talking about displaying data in a particular way but a
DataTable has nothing to do with display. It is for storage and that's
that. How you display the data is up to you. The same data can be
displayed in innumerable different ways. In a WinForms app, one of the
most common ways to display the contents of a DataTable is with a
DataGridView. You can choose which columns to display and merge cells
in that grid if desired. In short, you're looking in the wrong place
to do what you want to do. It is a presentation issue, not a data
issue. –
jmcilhinney

How to implement drag drop of CSV files into C# WPF and using the data in a datatable?

I've spent some time searching online for something that might help me but couldn't find exactly what I need. I found lots of examples with Windows forms but not with WPF.
I've got an army of CSV files. Each files contains only one datasheet, and the sheet only contains two columns. First column contains headers (the same for each CSV file) and the second column contains data. I'm trying to write something where the user can drag up to 5 CSV files into an area on the form and the application visualizes the first column and then up to 5 columns of data next to each other for comparison.
Anyone who can point me in the right direction will be much appreciated.
Thanks
I don't have anything handy in the way of easily getting the CSV data to the database - perhaps someone else will drop in. Adding support for drag'n'drop is a fairly simple matter of setting AllowDrop to true on the target container and then implementing its Drop event handler along the lines of
private void FilesDropped(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
...
There are at least 3 parts to this problem:
Enabling Drag & Drop support in your app (see 500 - Internal Server Error's answer for this)
Reading the selected CSV files. For this you might check out Mike Stall's DataTable project on Github
Displaying a DataGrid in WPF bound to the data returned in step 2.
For step three, these prior SO questions might be of value to you:
Generating columns dynamically in the WPF DataGrid?
WPF DataGrid Good Tutorials?
Binding WPF DataGrid to DataTable using TemplateColumns

Exporting a ListView to Excel

I have a ListView that I need to export to an Excel spread sheet. There a bunch of documentation to export GridViews and to a lesser extent ListViews to Excel but not so much in terms of doing some customization before export.
Before exporting I need to prep the data, as a) only a subset of the columns need to be exported b) some of these columns are editable textbox controls.
Doing a simple export without preping the table means that the control gets exported as well which obviously looks really ugly.
At the moment I can iterate the controls and remove the formatting from both the Grid and ListViews thus solving b.
In the GridView and I can just set some the columns I want to hide using Visible = false; and then set back to true when I have finished.
The thing I can't figure is how to do this with the listview.
I am wondering what is the best way to do this as I am kind of stuck.
Thanks,
Michael
I actually think this is a dumb question. If I want to export the list view to excel then i'll export all columns removing the unnecessary formatting. Then users can do any customization of the columns in Excel.
To do any customization of the data prior to export then I should use the underlying data types.
The above approach (admittedly my own) is simply bad.

Categories

Resources