Master/Detail Using a Selectable Master GridView with a Details DetailView (C#) | Microsoft Docs
You can customize a detail view in the GridView. I suggest you to go through documentation for Working with Master-Detail Relationships in. WinForms Data Grid: Master-Detail Relationships. DevExpress 6 ปีที่แล้ว implement a cardview within another grid control using a Master-Detail relationship. cardview within another grid control using a Master-Detail relationship. Learn more on our website jogglerwiki.info
We will have two data entities, User and Project. As a cardinality, we have a many-to-many relationship, so one user can be related to many projects as a project can have several users. Also I will create a class called DataService that will create a couple of values that will represent our data. Creating a data source First we will create a Project class. In the default constructor we will set the project status property to new. Except for this, we will have three properties, an ID, project name, and a project status.
It has several properties and one public method. The method returns the number of associated projects of the current customer instance. Next to come is a class that will create several instances of the model classes and return them via a method.
ASPxGridView master-detail data presentation with context menus - CodeProject
In this way we can have easily all the data we need for our example. The code I used is following, you can add other data if in search of a particular behavior.
The next thing to care of is the web page itself. Modify the properties in order to match the following: Now we need to bind the grid and we will do it from the code. In order to be able to show the changes, we will save our data in a session and in order to ease this operation we will create a property that will perform all of this check and operations for us. This is not a technique that you will use in a real life application, because probably you will recall the data from the database at a certain point and eventually cache it.
As explain this is not the goal of this article, I will just mention it.
Defining a detail grid In order to add a detail grid to a master grid, we first need to define a template for the detail row.
Close properly both sections. Also do not forget to set the ShowDetailRow property to true. At the end your code should look like this.
WinForms Data Grid: Master-Detail Relationships
Before that we will check for each row in the master grid if there is the data for the detail grid and if not, hide the plus sign. VisibleIndex, "ID" ; if! In order to get the row value we will use the argument that is passed to the event which contains the currently processing row visible index and with that information retrieve the value of the ID field of that row.
The following code gvMaster.
c# - DevExpress Datagrid Master-Detail Multiple List as a Datasource - Guidance - Stack Overflow
Then we will retrieve the User class instance for a given ID and check if it has project. In case that this user is not associated to any project we will hide the plus sign button for that row with by setting the argument property ButtonState to hidden.
Now let's bind detail grid. Each time the user click's on the plus sign, a callback will be automatically generated by the grid and on the server side, binding event for the detail grid will be raised. OnBeforePerformDataSelect event in the detail grid is the right one for indicating the data source on which the current detail grid should be bind. Define the previously mentioned event: Then DevExpress grid comes in our help with GetMasterRowKeyValue method, which as it's name says, will return the key value of the master grid in which our current detail grid is defined.
Whit that value, which is basically the User ID, we can retrieve the necessary data which we will set as a DataSource of the current detail grid.
- Editorial Note
- Your Answer
That's it, your master detail grid should work now and this is how it should look like. If your solution is not looking completely the same do not worry, important is that it compiles and shows the data correctly for now. You can see that I'm constantly pointing for the detail grid the current fact.
This is important, because we can have multiple detail grid's in the page, so we always need to refer to a proper object. Always think about that when you are working with detail grid. Adding a context menu to the detail grid This is a bit more complicated task but as you will see a quite simple way to achieve this.
Start with adding a client side event ContextMenu on the detail grid: As on the server side, the will fire behind a certain event.
We need to assign a function name that we are planning to execute on client side once the event is fired. Two parameters will be passed to our function, first is the control that generated the event sender and the event arguments. Each event and control has it's own arguments.
Master/Detail Using a Selectable Master GridView with a Details DetailView (C#)
DevExpress controls are very function rich on client side, and you can consult the documentation to check all available client side events, functions and properties.
If you are interested in the other controls, just browse the interested control namespace that ends with Script. In the page header or in a separate. If it is a grid row we should popup a context menu. We can refer to the control on the client side by the ClientInstanceName that we defined for that control in the aspx file, this is done in this example. Each time you put a DevExpress control in your page, you will automatically have at your disposition an utility class called ASPxClientUtils containing several methods that can help you reducing your js code.
Now each time you right click the detail grid row, a context menu that you defined will be shown. What we are missing is the action that needs to be performed once the user chooses a context menu.
As we are going to use a callback method of the detail grid to process the action on the server side, we need to find out the right detail grid that needs to be updated.
For this tutorial, let's have the selected row be highlighted with a yellow background. As with our earlier tutorials, let's strive to keep the aesthetic-related settings defined as CSS classes.
Therefore, create a new CSS class in Styles. As we discussed earlier, the GridView's SelectedValue property returns the first data key value for the selected row. When the page is first visited no row is selected, so the GridView's SelectedValue property returns null.
In this tutorial we examined using a selectable GridView to house the master records and a DetailsView to display details about the selected master record on the same page.
Starting with the next tutorialwe'll begin our exploration of customized formatting with the GridView, DetailsView, and FormView. We'll see how to customize the appearance of these controls based on the data bound to them, how to summarize data in the GridView's footer, and how to use templates to obtain a greater degree of control over the layout.
Scott works as an independent consultant, trainer, and writer. He can be reached at mitchell 4GuysFromRolla. Special Thanks To This tutorial series was reviewed by many helpful reviewers. Lead reviewer for this tutorial was Hilton Giesenow. Interested in reviewing my upcoming MSDN articles?