Data Views

The CollectionView class represents a view for grouping, sorting, filtering, and navigating data collections.

The object model used in the CollectionView class is similar to the one defined by .NET's ICollectionView and IPagedCollectionView interfaces.

To create data views, follow these steps:

  1. Create a new CollectionView.
  2. Set the sourceCollection to an array containing the raw data.
  3. (Optional) Add items to the sortDescriptions array to define how the data should be sorted.
  4. (Optional) Add items to the groupDescriptions array to define how the data should be grouped.
  5. (Optional) Set the filter property to a predicate function that selects the items that should be included in the view.
  6. (Optional) Set the pageSize property to define the number of items in the view, and use the moveToPage method to select the current page.
  7. Use the items array to get the output view.
  8. Use the currentItem property to get a reference to the item that is currently selected, and use the currentChanged event to detect a change in selection.
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1MvcController : Controller
    {
        // GET: CVCreatingViews
        public ActionResult CVCreatingViews()
        {
            return View();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<h1>
    @Html.Raw(Resources.C1Mvc.CVCreatingViews_Title)
</h1>
 
<p>
    @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text1)
</p>
<p>
    @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text2)
</p>
<p>
    @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text3)
</p>
<ol>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text4)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text5)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text6)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text7)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text8)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text9)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text10)
    </li>
    <li>
        @Html.Raw(Resources.C1Mvc.CVCreatingViews_Text11)
    </li>
</ol>