Using Linq in column bound

10 posts, 0 answers
  1. Michel
    Michel avatar
    44 posts
    Member since:
    Sep 2011

    Posted 16 Jan 2017 Link to this post

    Hi,

    I don't know how I can use linq query in bounding a grid column to display a value.

    If I use this code

        @(Html.Kendo().Grid<MYMODEL>(Model)
        .Name("myObject")
        .Columns(columns =>
        {
            columns.Bound(ViewBag["myList"].Where(p => p.Value == "2").Text);

    I have an error : "cannot  use lamba expression without casting before.

    Any help will be very much appreciated

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 16 Jan 2017 Link to this post

    Hello Michel,

    As the error states, you need to cast the object from the ViewData to a type that supports lambda expressions (for example, if it is a List: (ViewBag["myList"] as List<YourClass>).Where(p => p.Value == "2"))

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Michel
    Michel avatar
    44 posts
    Member since:
    Sep 2011

    Posted 16 Jan 2017 in reply to Konstantin Dikov Link to this post

    Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.

    But the thing that I want to achieve is a bit more complicated.

    I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even is CODE is stored in the database table).

    Controller.cs

    public async Task<IActionResult> Index()

          ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m =>                                                                                (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");

    Razor View

           ...

    columns.Bound(model => model.myCode); // For testing purpose

            columns.Bound((((ViewBag["arType"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
    // Error when executing

    Any suggestion ?

    ...

  4. Michel
    Michel avatar
    44 posts
    Member since:
    Sep 2011

    Posted 16 Jan 2017 in reply to Michel Link to this post

     

    Sorry some mistake were in the previous post.

    Thanks for this Konstantin. I need just to figure out the meaning of the message. Sorry.
    But the thing that I want to achieve is a bit more complicated.
    I have a list in my controller coming from a table in a database and I want to use the ViewBag for selecting the value in a list in editor popup (that works well) but want to display the LABEL and not CODE in the grid (even if CODE is stored in the database table).
    Controller.cs
    public async Task<IActionResult> Index()
          ViewData["myList"] = new SelectList(_context.MYTABLE.Where(m =>                                                                                (m.FIELDNAME == "MYTYPE")),"CODE", "LABEL");
    Razor View
           ...
    columns.Bound(model => model.myCode); // For testing purpose
            columns.Bound((((ViewBag["myList"] as List<SelectList>).Where(p => p.DataValueField == "LOC")) as SelectListItem).Text);
    // Error when executing
    Any suggestion ?

  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 16 Jan 2017 Link to this post

    Hello Michel,

    I am not sure that I understand what is the result that you want to achieve. Within the bound column you could pass a string value that will match the name of the field that will be bound to the column.

    If you want the column to be bound to one field, but to display the value for another field, the records in the grid should contain both values. The first field will be bound to the column and the second field can be displayed through a ClientTemplate (for example: ClientTemplate("#=SecondFieldName#")).

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. Antony
    Antony avatar
    5 posts
    Member since:
    Sep 2017

    Posted 15 Nov 2017 in reply to Michel Link to this post

    Hi Michel,

    Were you able to find a workaround this situation? I've come across the same problem. 

  7. Michel
    Michel avatar
    44 posts
    Member since:
    Sep 2011

    Posted 15 Nov 2017 in reply to Antony Link to this post

    It is quite long time but in looking at my code here is the view (forgot the FooterTemplate if you don't need it):

    columns.ForeignKey(model => model.CATEGORY_MEM, (System.Collections.IEnumerable)ViewBag.arCategory,"CODE_LFB", "LABEL_LFB") .ClientFooterTemplate("Total Branche : #=count#");

     

     

    And in the Controller :

    var category = _context.VG_LSTFEEDBACK_LFB.Where(f => f.NUMBER_LFB == "CAT").
      Select(f => new
      {
        CODE_LFB = f.CODE_LFB,
        LABEL_LFB = f.LABEL_LFB
      });
    ViewData["arCategory"] = category.ToList();

     

    The VG_LSTFEEDBACK_LFB is a SQL Table with different List value, it is why a use a where clause.

    Hope it help's.

     

     

     

  8. Michel
    Michel avatar
    44 posts
    Member since:
    Sep 2011

    Posted 15 Nov 2017 in reply to Michel Link to this post

    SQL Server Database MPD

    CREATE TABLE [dbo].[VG_LSTFEEDBACK_LFB](
     [GUID_LFB] [uniqueidentifier] NOT NULL,
     [NAME_LFB] [varchar](100) NULL,
     [NUMBER_LFB] [varchar](25) NULL,
     [CODE_LFB] [int] NOT NULL,
     [LABEL_LFB] [varchar](25) NULL,
     CONSTRAINT [PK_VG_LSTFEEDBACK_LFB] PRIMARY KEY CLUSTERED

  9. Antony
    Antony avatar
    5 posts
    Member since:
    Sep 2017

    Posted 15 Nov 2017 in reply to Michel Link to this post

    Thanks, using it as a foreign key worked. 
  10. Stefan
    Admin
    Stefan avatar
    2913 posts

    Posted 17 Nov 2017 Link to this post

    Hello, Michel, Antony,

    Michel, thank you for sharing the code and helping the community it is highly appreciated.

    Antony, I`m glad to hear that the foreign key was helpful.

    I can also suggest checking our foreign key demo and resources as they may prove helpful: 

    http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn

    Kendo.Mvc.UI.Fluent/GridColumnFactory

    https://docs.telerik.com/aspnet-mvc/helpers/grid/how-to/editing/configure-foreignkey-columns-combobox-edit-template


    Regards,
    Stefan
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top