12.09.2019
Posted by 
Active1 year, 9 months ago

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)

RadGridView for WinForms Hierarchy Overview In this video you will learn the various ways you can display hierarchical data in a RadGridView. (Runtime: 12:13), gridview-hierarchical-grid-binding-to-hierarchical-data-programmatically 001.

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.

demonplus
4,1479 gold badges37 silver badges53 bronze badges
natnat

Hierarchical Datagridview

1,2173 gold badges26 silver badges48 bronze badges

3 Answers

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 TreeView with Columns or FlexGrid 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? Patch age empire 3 francais pour.

PS: I'm not affiliated with ComponentOne, but I've used FlexGrid in a project.

NilzorNilzor
9,58717 gold badges75 silver badges142 bronze badges

Yes this can be done.

Set a data relation, either in the dataset designer or in code between the two tables using the foreign key - parent key columns.

If in the dataset designer, set the nested property to true. This will then give you child records under each parent row in the datagrid view.

demonplus
4,1479 gold badges37 silver badges53 bronze badges
Nael MahfouzNael Mahfouz
Deepthi KrishnaDeepthi Krishna

Not the answer you're looking for? Browse other questions tagged c#.netdatagridviewexpand or ask your own question.



Introduction

Nowadays display nested grid view (tabular data) is an essential part in web development. There are lots of 3rd party plugin like Telerik grid view for display hierarchy and nested data. But most of the developer avoid 3rd party plugins.Grid
Now in the MVC framework, we have full control over HTML. We can display multilevel hierarchical data without using any 3rd party plugin. In one of my previous article I have shown, how to display grouping data (order and order items) in asp.net MVC application. Here in this article, we will see how we can display multilevel nested grid view in asp.net MVC application.

Just follow the following steps in order to implement 'Displaying multilevel nested grid data in asp.net MVC'.

Here In this article, I have used Visual Studio 2013

Step - 1: Create New Project.

Go to File > New > Project > ASP.NET Web Application (under web) > Entry Application Name > Click OK > Select Empty template > Checked MVC (under 'Add folders and core references for' option) > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create a table for store data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have created a table (see below image) for store employee data with superior/subordinate relationships in an organization for show multilevel nested grid data.

Employees Table

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Create a custom HTML Helpers.

Here in this application, we have created a custom HTML Helper for generating a table for show multilevel nested grid data.
[N.B. if there is no folder named 'App_Code' in your solution, Please follow the step for add App_Code folder. Go to solution explorer > Right click on your project > Add > Add ASP.NET Folder > App_Code.]
Right Click on App_Code folder under Solution Explorer > Add > New Item > Select MVC5 View Page (Rozar) > Enter Name (here 'NestedData.cshtml') > Add.
Write this below code to your newly created view under App_Code folder.

Step-6: Create an MVC Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete 'empty MVC Controller'> Add.
Here I have created a controller named 'HomeController'

Step-7: Add new action into your controller for show multilevel nested grid data.

Here I have added 'Index' Action into 'Home' Controller. Please write this following code

Step-8: Add an image into our application.

Before design the view, we will add an image in our application for use as collapse/expand icon.
Right Click on project from solution explorer > Add > new folder > Rename 'Images' and then Right click on that folder > Add > Existing Item.. > select image > Add.

Step-9: Add view for your Action and design.

Right Click on Action Method (here right click on Index action) > Add View.. > Enter View Name > Select View Engine (Razor) > Add.
HTML Code

Hierarchical Data Grid View Visual Studio

Here I have created two React components named 'EmployeeGridTable' and 'EmployeeGridRow'.

Hierarchical Data Grid View C#

Step-10: Run Application.