-1

I have a DevExpress gridview which works as it should, but... in the binded model it's also a collection of "VisitTypes" i'm not sure how to handle..

It's a list (collection) of type "VisitTypeSummary".

What i'm trying to do is add a new column for each item in this collection. The Collection looks like this :

  1. VisitTypeId
  2. Name
  3. Count

So the result should look something like this (if the nr. of items is 3) :

  • Col1 : Area
  • Col2 : Name
  • Col3 : Address
  • Col4 : VisitTypeSummary[0].Name
  • Col5 : VisitTypeSummary[1].Name
  • Col6 : VisitTypeSummary[2].Name
  • Col7 : LastVisited

etc.etc.etc....

The issue is i dont know how to actually.. access the Model in the PartialView, and i'm trying to accomplish this by doing a foreach(var item in Model.VisitTypeSummary) but it seems and smells wrong..

Here is some code :

var grid = Html.DevExpress().GridView(settings => {
-- etc -->

settings.Columns.Add("AntButikkMedBesoeksKode");
settings.Columns.Add("BesoeksProsent");

foreach (var VisitTypeSummary in the-model-i-cannot-figure-out) )
    {
    // add new column to the gridview                                                      
    }

settings.Columns.Add("AntKundeBesoek");
settings.Columns.Add("SumBesoekForSelger");
settings.Columns.Add("SnittBesoekPrDagSelger");

// and bind it in the end..
@grid.Bind(Model).GetHtml()

So.. anyone know how i can do this ? Been googlin' for a while now, but no success.

Terje Nygård
  • 1,233
  • 6
  • 25
  • 48

1 Answers1

1

Sounds like you are describing a Master-Detail relationship. Each record in your model has a collection of VisitTypeSummary.

See the DevExpress Demo here

I would suggest putting the VisitTypeSummary collection in the detail view, while the main model is displayed as the Master dataset. The detail view will then have a single column listing the names of the VisitTypeSummary, you could add more columns later if the class expands.

This will reduce the total number of columns in your Master Grid View thereby simplifying the user interface and enhancing usability.

EDIT: As per my comment, if you really need those columns in the grid, you could use a for loop to add them:

for (int i = 0; i < VisitTypeSummary.Count; i++)
{
    settings.Columns.Add(string.Format("VisitTypeSummary[{0}].Name"), i);
}
Charl
  • 113
  • 7
  • That makes sense, except that i kinda need it to stay in the same grid and not including it as a master-detail, where the details in a way is... not connected directly to the grid as it would have been if i could have inserted the columns directly.. So therefore i still need to figure out how to access the data in the partial view so i can manually add them to a column i'm afraid. :) – Terje Nygård May 28 '14 at 07:48
  • OK, in that case I guess using a for loop is your best option. When adding the column you can use standard dot notation: settings.Columns.Add(string.format("VisitTypeSummary[{0}].Name"), i); where i is your counter in the for loop (for (int i = 0; i < VisitTypeSummary.Count; i++)) – Charl May 28 '14 at 08:33