-
Hierarchical Data Grid View Select Row카테고리 없음 2020. 2. 21. 10:19
Unfortunately, this intrinsic.NET control does not provide you with any built-in features to merge cells, but you can imitate cell merging with DataGridView's events, methods and properties. In this article we will consider two well-known solutions to the problem of merging cells in WinForms DataGridView and analyze their pros and cons. How to merge DataGridView cells by removing cell bordersIn many cases the tabular data you display in WinForms DataGridView comes from relational databases. The tables we visualize in DataGridView may contain repeated field values inside a column, and you may want to merge them into one big cell for better visual representation on the screen.It is relatively easy to find solutions in which DataGridView cells are merged in columns automatically. The idea of this approach is to blank out repeated cell values on the screen and remove the corresponding borders of DataGridView cells belonging to one merged cell dynamically. DataGridView provides convenient tools to do that – the OnCellFormatting and OnCellPainting methods which you can override.
As an alternative, you can inherit the DataGridViewTextBoxCell class and override its GetFormattedValue and Paint methods. The following thread on the MSDN social network suggests several implementations of this idea:Perhaps, the most useable solution in that thread is the DataGridViewMergedTextBoxColumn class posted by Felice Ferri. This approach allows you to merge DataGridView cells vertically only in particular columns but not in every column of the DataGridView.
This looks good as a static screen and even allows you to specify any horizontal and vertical alignment for the texts in merged cells in the DataGridView. But the main drawback of this approach is that you cannot select these merged cells – neither with the mouse nor with the keyboard. The keyboard navigation in the DataGridView becomes chaotic if you use the cursor movement keys on your keyboard.Add to this that using custom drawing to repaint DataGridView cells makes the whole control drawing significantly slower. This becomes noticeable while you are resizing a form together with a DataGridView with cells merged horizontally this way, even if we have 2 or 3 cells merged horizontally in the viewport.
Hierarchical Data Grid View Select Row Programmatically
And this also slows down the scrolling in the DatGridView, which is slow enough by default (to find out more, read the article on this website). Cell merging in a DataGridView alternative, iGrid.NETAs you can see, WinForms DataGridView does not support cell merging out of the box. Even the rich set of DataGridView members does not allow you to implement a viable substitution for true merged cells you could use in real-world applications. If you need to merge cells in DataGridView, perhaps it is time to consider an alternative grid control with native cell merging capabilities such as our iGrid.NET control.10Tec iGrid for.NET allows you to merge individual cells using the simple SpanRows and SpanCols cell properties.
In addition to this manual way of merging cells, you can use methods like MergeCellsInCols to merge cells with repeated values in columns automatically. The MergeCellsInCols method is free from the drawbacks of the DataGridViewMergedTextBoxColumn class and similar solutions described above. It takes into account the specified column hierarchy and even allows you to assign various cell styles to merged cells created automatically to format them with colors, fonts, and the like. IGrid merged cells are integrated seamlessly into the traditional keyboard navigation model and all mouse-based selection operations, like drag select in multiselect mode. Even sorting and grouping are allowed in iGrid if they do not lead to breaking existing merged cells (or you can dynamically remove cell merges before sorting/grouping to allow them with one call like the UnmergeAllCells method). All this allows you to integrate merged cells with other features of iGrid like tree grid to implement sophisticated interfaces like the following example of the project management tool in the form of a Gantt chart.
I am trying to construct a datagridview that will show a number of rows with a particular set of fields, but also allow me to show some 'subrows' ie rows that can be expanded below the parent row in question (if that parent row has these children - part of the datasoruce) the only real problem is that the expanded rows will have completely different fields(columns)is this possible with a standard datagridview in a windows form?I am presuming somehow I would have to shoehorn in a new datagridview in a row of the parent grid.We do have the Syncfusion suite which includes the gridgroupingcontrol. But after much hair pulling and other pains. I have given up on that completely. Short answer: No.
You need a custom component for this.There are a lot of good efforts out there on making a hierarchical treeview. Like in the CodeProject article or from Component One (paid). I understand you want different fields on the subnodes though, and I'm unsure to what extent these controls support that. I know FlexGrid support some customization of columns for different rows, but it will probably involve quite some manual work - databinding is not well supported.Finally, you might want to consider if your idea of a customized GridView is the best approach to your problem in the first place. Could a Master-Detail-view be a better approach?PS: I'm not affiliated with ComponentOne, but I've used FlexGrid in a project.