Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have something like

ViewData["getServices"] = (from p in dbObj.gen_crm_consolidate
                                       where p.service_id == sID && p.pdate >= sDate.Date && p.pdate <= eDate.Date
                                       orderby p.pdate descending
                                       select p).ToList();
return PartialView("_ServicePartial", ViewData["getServices"]);


and in View i am using this as
var q = (List<DM.Models.DB.gen_crm_consolidate>)(ViewData["getServices"]);
                           foreach (var item in q)
                           {


It is working perfectly fine. I have almost 10-12 columns in ViewData and just i want to check weather complete column is empty or not and if it is empty i will not show it in view.

What I have tried:

I tried to access value of q from viewdata but nothing happens.
Posted
Updated 8-Feb-17 23:36pm
Comments
Karthik_Mahalingam 7-Feb-17 7:53am    
what is complete column ?
not clear, provide more info.
Member 12583662 8-Feb-17 5:36am    
foreach (var item in q)
{


@Convert.ToString(string.Format("{0:dd/MM/yyyy}", @item.pdate))
@item.active_base
@item.charged_base
@item.total_calls
@item.total_mou
@item.daily_ivr_unique_user
@item.total_pulse
@item.actrevenue
@item.Rrevenue
@item.total_revenue

}
Member 12583662 8-Feb-17 5:38am    
Action code is already given. and this is the view. View changes based on different services selected. For some active_base is null and for they have values. I want to check if entire column is null than it should not be seen on screen.
Karthik_Mahalingam 8-Feb-17 5:58am    
entire colum or row ?
Member 12583662 8-Feb-17 6:58am    
it will fetch and show date wise data from 1st to 30th. want to exclude if Entire column is null.

1 solution

refer this example and build it.
Location and Age columns are empty and 0 values respectively.
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
    <link href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet" />
    <script src="jquery.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    

    <script>
        var table;
        $(document).ready(function () {
             table = $('#example').DataTable({                
                "paging": false
             });
             var columnsCount = table.columns()[0].length;
             var data = table.rows().data();
             var columnIndexToHide = [];
             for (var j = 0; j < columnsCount; j++) {
                 
                 var icount = 0;
                 for (var i = 0; i < data.length; i++) {
                     var val = data[i][j];                      
                     if (val != '' && val != '0') // hide for empty and column having 0 value.
                         break;
                     else
                     {
                         icount++;
                         if (icount == data.length)
                         { columnIndexToHide.push(j); break;}                         
                     }
                     
                 }
             }
             for (var i = 0; i < columnIndexToHide.length; i++) {
                 table.column(columnIndexToHide[i]).visible(false)
             } 
        });
       
    </script> 
    </head>
<body>
    
     
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Location</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td></td>
                <td>0</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td></td>
                <td>0</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td></td>
                <td>0</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td></td>
                <td>0</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>
            
        
        </tbody>
    </table>
    

</body> 
</html>
 
Share this answer
 
Comments
Member 12583662 9-Feb-17 6:31am    
Hi Karthik, First of all thanks alot it is perfectly working fine. But can we write the same code in View part where we mentioned the TH and TR code. or is there some compexity to write code there.
Karthik_Mahalingam 9-Feb-17 7:06am    
use this concept and slight modification would do the effect.
Member 12583662 9-Feb-17 7:33am    
it is working, but my question is can i write some logic in view only.
Karthik_Mahalingam 9-Feb-17 7:39am    
Yes
Member 12583662 9-Feb-17 10:53am    
Can you help, as this code is taking time due to multiple for loops.
Any other solution is required.

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