Suitable control to select fieldnames when importing tables - c#

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.

Related

DataGridView Column with multiple controls (Radiobuttons, check boxes, list drop downs, etc.)

I'm interested in creating a DataGridView Column that contains multiple control objects. Specifically, I'd like to build a Column that contains: a text field, a list dropdown, a few checkboxes, and a few radio buttons.
I did some research and came across this page, http://www.codemag.com/article/0707061, which gives a general idea for what I need to do but is it little bit more complex than what I'm looking for. I've not been able to find any examples of adding multiple control objects to a single column.
I'm looking for other resources that I can reference to help me with this task.
This is being done in winforms.
Looks like I'll need to build a custom DataViewGridCell for each control that I want. I can then set the column's .CellTemplate to that Cell. Can a column have multiple CellTemplates?
The short version is like you suggested. It would be to create a custom ViewCell that Inherts from DataGridViewCell contains each of the controls you want to add. Then set the template to that column.
DataGridViewColumn column = dataGridView.Columns[indexForYourColumn];
DataGridViewCell cell = new YourCustomDataGridViewCell();
column.CellTemplate = cell;
Some specific resources to use CellTemplate, CustomControls1, CustomControls2

How to merge two cell in datagrid

I'm writing spreadsheet based on wpf datagrid, and I want to add functionality like merging two cells (excel like). Can anyone tell me if it's possible in datagrid at runtime and if so, how to do it?

How to adapt column width of Grid's exported Excel?

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.

Merging cells of a row of data grid view in C# windows application

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.

C# Winforms DataGridView with sorting/filtering like Ms Excel

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.

Categories