Search code examples
c#winformsdatagridview

How to get a DataGridView with vertical Columns


In my Winform C# application I have a DataGridView that bound with DataTable and have many records. I want to display details of the selected row (of DataGridView ) in the following Format (see Image).What Control or (Trick) should I use to achieve this.

enter image description here

In other words: “First Column of the Table should have Columns name and 2nd Column should have its corresponding values”


Solution

  • Typically, I wouldn't do this in a DataGridView, its really intended to display multiple records and you only want to display one record at a time. I can suggest two alternate ways of dealing with this:

    The typical approach if you know the fields to be displayed ahead of time would be to lay out a set of individual value displaying labels and other simple controls at design time. This has the following advantages:

    • The position of things is more flexible
    • The type of control used for each individual field can be customized. For example, a booean value might display with a check box, rather than with text.
    • The labels preceding the value can be localized into the users language. Even if not an international application, its sometimes useful to display labels that are different from the database schema names of the columns.

    If you don't know the fields to be displayed at design time, or don't want to bother laying them out, use a PropertyGrid control instead of a DataGridView. I've never actually tried this with a DataTable as a data source, but it looks like there is a well developed answer for that at C#/winforms: how to best bind a propertygrid and a System.Data.DataRow