Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi "column" DataGridView C#

I have a DataGridView that's bound to a DataSet. It has columns DateCreated, Weight, DateUsed. Those three columns do not take up much horizontal space on DataGridView that is nearly full screen. Is it possible to have those columns wrap back to the top if the view is wide enough to support a second group of those colums. So across the header it would read DateCreated, Weight, DateUsed, DateCreated, Weight, DateUsed. Then when a row is clicked, only 3 cells would be highlighted, not all six. Is there something that can provide this functionality?

like image 460
Wesley Avatar asked Oct 25 '22 17:10

Wesley


2 Answers

I don't know of anything like that out of the box.

You could create that effect yourself. You would need to decouple the actual data from the presentation of the data. Instead of a single DataRow per grid row, you'll need to merge them together to show multiple rows on a single row.

So your helper method might do the following:

  • Compare the width of the DataGridView to the number of rows
  • Determine how many DataRows columns would be visible
  • Build a new DataTable with extra column sets to represent each column
    • Example: DateCreated1, Weight1, DateUsed1, DateCreated2, Weight2, DateUsed2, etc.
  • Populate the DataTable with the original DataSet
  • Bind the DataGridView to the new table

Some considerations: can users sort the data? If so, you'll have to write that yourself.

Do you need to be able to add new rows using this view? That sounds very difficult with this kind of view.

like image 133
Paul Williams Avatar answered Nov 15 '22 12:11

Paul Williams


I realize you posted this question in April, and so you've probably already found a solution or else moved on to something else. However, I recently wrote a control that does basically just what you describe, and you can download it freely from BitBucket here:

https://bitbucket.org/dtao/taocontrols/downloads

Screenshot of SplitDataGridView control

If you're interested, give the SplitDataGridView control a shot and let me know what you think.

like image 40
Dan Tao Avatar answered Nov 15 '22 12:11

Dan Tao