we are using the Telerik's Grid component with ASP.net (the package is called ASP.NET AJAX).
The grid is very comfortable, it offers a Export To Excel functionality.
Unfortunately the ways of customizing the Excel seem to be limited.
The question is:
How can I make the column width of the resulting excel fitting to the content of the widest cell?
Public Sub ExportGridToExcel(vGrid As RadGrid)
mIsExporting = True
vGrid.Rebind()
vGrid.ExportSettings.Excel.Format = GridExcelExportFormat.Biff
vGrid.MasterTableView.ExportToExcel()
End Sub
See here or here how to access and modify columns before export. On getting the longest string - I think you would need to query the datasource and traverse it to know. The control would have no way of knowing what data it will receive. If you expect exports to be common you can consider doing this calculation only once when data binding the grid, and storing the preferred widhts of the columns in the ViewState, Session or something. Of course, if your data is rather static, you can consider storing this infomormation about the database fields content in the database itself.
Related
I have a list view in WinForms that works and looks fine. However, the grid lines are formatted in such a way that there's no separation between the column headers and the first row of data. This makes it look like the first row of data is part of the column headers. You can see what I mean here:
Is there any way I can format the list to stop this from happening, and make it look 'proper'? Thanks for any help.
I see you're displaying "data" to users in a grid - except you're using a ListView - which is really intended for filesystem display, not data.
I recommend you switch to using DataGridView (avoid System.Windows.Forms.DataGrid as it's older and doesn't let you (easily) control the data being displayed).
Also, protip for usability: if a table cell contains numeric data then it should be formatted with Right alignment, otherwise give it a Left alignment - avoid Middle/Center alignment in non-header table cells as it makes it difficult to visually scan a table.
I'm using a GridView that is bound to an object (an entity). The grid can be filtered and otherwise customized in terms of the data it shows. The rows, columns and more specifically cells will be formatted using some rules, but also from the user specifically setting formatting options.
My questions is about the best way to recall the chosen formatting for specific cells. Currently my best method is to store an id for each cell that looks up all of the formatting for that cell (i.e. ForeColor, BackColor, FontWeight etc).
Another way would be to create a new table (called say Formatted_Cells) that stores the id and column name of the table in question, and then the formatting options. This would involve checking the Formatted_Cells table each time a cell is processed to check for formatting. It is quite a bit of processing (similar to the above method). If I did this I could flag any rows that have custom formatting, and if they do not I wouldn't need to check the Formatted_Cells table which does provide an advantage over remembering formatting for EVERY cell, when the vast majority will not have any custom formatting.
Is there a better way? I don't think I can use Serialization to help as the grid is loaded from the database which may be modified elsewhere. But perhaps I'm missing something obvious?
Much appreciated.
I'd suggest an additional table also.
Two possible solutions come to mind:
1- Do the formatting in two passes. First load the grid normally, then read Formatted_Cells table from database, find styled rows in grid and apply special styling.
2- When quering database for Cells, add Formatted_Cells table with left join. When loading grid, check for additional columns that might come from left join, apply those. If no additional columns, style normally.
I'm creating a import module for excelsheet for a winforms program (C#).
I was wondering if there is a control (somekind of gridview of listview) that supports a dropdownlist as a columnheader. This for assigning fieldnames to columns at runtime before importing an excel table.
If there is no control for it, does anyone knows some good examples/tutorials for creating a custom control that supports such a task.
Thanks for your input.
I think you will need to create a custom control, the datagridview supports comboboxes in its cells, you could make the first row all comboboxes and hide the column headers.
Example here.
Is it possible to merge two or more cells of a particular row of data grid view in C# windows application?
Quickedy quick Google search turns up this.
http://www.codeguru.com/forum/showthread.php?t=415930
But no, natively you can't - Datagridviews are not equal to HTML tables or Excel sheets. They simply take the datasource that is bound to them and (after some optional trickery) present the datasource in columns and rows.
You can create your own column class by inheriting the DataGridViewColumn class or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see Custom Column
Yes, You will have to use DataTemplate within a TemplatedColumn
and Bind the data to two separate textblocks within the template.
Hi I need a quick solution to do filtering/sorting using the Winforms DataGridView control just as in Excel.
I have reviewed the existing posts on this area but none seems to meet my needs.
I am populating my DataGridView manually - no data binding
The DataGridView columns already support sorting.
I would populate a DataTable with your data and then bind the DataGridView to myDataTable.DefaultView.
You can filter the rows displayed by setting myDataTable.DefaultView.RowFilter.
You could place Textboxes and/or Comboboxes above the DataGridView and update myDataTable.DefaultView.RowFilter as the input/selections change.
If you're looking for a Excel like filtering funcionality, check out this article:
http://msdn.microsoft.com/en-us/library/aa480727.aspx
Why don't use a cheap 3rd-party component? Even if you buy it, eventually it could really save your money. This DataGridView alternative with autofilter works very fast, and unbound mode is its main work mode. Plus it supports Excel-style AutoFilter.
Do you want something like this?
DataGridView-AutoFilter is a ready-made Nuget package, you just need to download it and follow this article, it is an easy and enhanced approach.
Microsoft has created a sample project for VB and C# where they show how to create what they say is an "auto-filter" plugin. I personally do not like it as it allows filter exact only so product and customer will work, open invoice where price > some value is not implemented
Sorting is implemented by using:
foreach (DataGridViewColumn column in MyDataView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.Automatic;
}
You do this when the grid has it's columns, if auto creating the columns… after binding.