Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I wants to count the total leaves of each users from the database.
and wants to display it in telerik gridview in Asp.Net MVC-3 (Aspx).

i have a gridview with two columns. first column displays the names of all the employees/users and 2nd column will display the total leaves of that users.

my database is as below

leave_master

leave_id (primary key)
user_id (foreign key with user_id of table user_master)
leave_days


user_master

user_id (primary key)
user_real_name


My Model files are as below

name - LeaveGridview.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;


namespace ProjectManagementSystem.Models
{
    public class LeaveGridview
    {
      
        public int user_id { get; set; }

        public string user_name { get; set; }

        public string user_real_name { get; set; }    

        public int leave_id { get; set; }
       
        public decimal leave_days { get; set; }
    
       
    }
}



2nd file

name - LeaveGridviewFetch.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectManagementSystem.Models
{
    
    public class LeaveGridviewFetch
    {
        

       
        public static IList<LeaveGridview> all()
        {
            IList<LeaveGridview> result =
                (IList<LeaveGridview>)HttpContext.Current.Session["leave1"];

            if (result == null)
            {

                HttpContext.Current.Session["leave1"] = result =
                    (from l in new ProjectManagementSystemEntities3().leave_master
                     select new LeaveGridview
                     {
                         user_id = l.user_id,
                         leave_id = l.leave_id,
                         user_real_name = l.user_master.user_real_name,
                        
                        
                      
                     }).ToList();
                
            }
            return result;
        }
    }
}



My contgroller file
name LeaveController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectManagementSystem.Models;
using Telerik.Web.Mvc;

namespace LeaveModule.Controllers
{
    public class LeaveController : Controller
    {
        public ProjectManagementSystemEntities3 pb = new ProjectManagementSystemEntities3();

  public ActionResult LeaveShow()
        {
            var leave_master = pb.leave_master.Include("user_master");
            ViewBag.leave_id = new SelectList(pb.user_master, "user_id", "user_real_name");
            return View(leave_master.ToList());
        }

 [GridAction]
        public ActionResult _SelectBatchEditing()
        {
            return View(new GridModel(LeaveGridviewFetch.all()));
        }

}
}


my view is

name LeaveShow.ascx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    LeaveShow
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<%  Html.Telerik().Grid<ProjectManagementSystem.Models.LeaveGridview>() 
                       .Name("grid")
                       .DataKeys(keys =>
                       {
                           keys.Add(l => l.leave_id);
                       })
                       
                       .DataBinding(dataBinding =>
                        dataBinding.Ajax()
                       .Select("_SelectBatchEditing", "Leave") // Leave is controller name
                       )
                       
                        .Columns(columns =>
                       {
                         
                           columns.Bound(l => l.user_real_name).Width(200); 
                         
                           columns.Bound(l => l.leave_days).Width(150);
                                                                          
                       })
                  

                       .Groupable()
                       .Pageable()
                       .Filterable()
                       .Render();
                       
                       %>


</asp:Content>


i wanna output like

name---------------total_ leaves
abc---------------- 10
xyz---------------- 12
pqr---------------- 06


So how can i do that?
Posted

1 solution

You write code to keep a summary of all leaves per person.
You bind that collection to your grid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900