Is it possible to have two column headers in Datagrid view using c#
I like to take a column header text and span it across to columns. How would you do this? I can't 开发者_运维知识库find a "Headercell.Span." Also is it possible to have two column headers?
Like this
| FULL NAME | DESC. |
| first | last | state | age |
DATA
The DataGridView
does not support this. You could eventually simulate a header using labels and prevent expanding columns more than label width.
You could probably do this by creating a template field.
In the header template, create your heading.
In the item template, just add <%#Eval ("YourDataItem") %> statements for every data item you want to include.
Ok, this may not be the most elegant of solutions (or it very well could be, not sure)...
- Create a Panel
- set AutoScroll to True (this setting is key)
- Add DataGridView to Panel
- make sure only verticle scrolling is enabled, otherwise you'll get two horizontal scroll bars
- Add a TableLayoutPanel to the Panel
Now position the TLP (TableLayoutPanel) at the very top of the inside of the Panel. Make sure there is only a single Row, and add as many colums as you need. Insert a Label into each of the colums, and don't worry about alignment just yet.
Now, position the DGV (DataGridView) just underneath the TLP within the Panel, and align the TLP columns to the DGV columns (start with the right-most column separator in the TLP, then work your way left).
If your Panel is wider than both of the DGV and TLP, then shrink it down until a scroll bar appears (this is where the Panel AutoScroll comes in). You shouldn't have to run the program to see the scroll bar.
Try to scroll, and you should see that both the TLP (and Labels within it) and the DGV move together.
Thats it.
精彩评论