locked
Select Column names based on number of days in the selected month RRS feed

  • Question

  • User325035487 posted

    I have 31 columns. Titled D1,D2,D3...D31

    int days = DateTime.DaysInMonth(y1, m1);
    
    var data = db.Query(sql, unitid, true); \\Returns the data set with my 31 columns
    
    @foreach (var r in data)
    {
    <tr>
    
          @for (int j = 1; j <= days; j++)
               {
                 string duty = "D" + j;
                 <td>
                     @r.duty
                 </td>
               }
    </tr>
    }
    

    This wont work as @r.duty looks for a column named duty in data. What I am trying to acheieve is based on the number of days in the month. For example if 28 days I want only 28 columns D1-D28 to populate data inside the <td></td> tags for each row in data set. How to achieve this?

    Any thoughts are highly appreciated

    Saturday, July 11, 2015 8:25 PM

Answers

  • User-821857111 posted
    @foreach (var r in data)
    {
    <tr>
    
          @for (int j = 1; j <= days; j++)
          {
              <td>@r["D" + j]</td>
          }
    </tr>
    }

    You can use an indexer to access dynamic properties instead of dot notation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 1:57 AM

All replies

  • User325035487 posted

    My temporary fix is to replace the for loop with hardcoded like this

                                <td>@r.D25</td>
                                <td>@r.D26</td>
                                <td>@r.D27</td>
                                <td>@r.D28</td>
                                @if (r.D29 != null) {<td>@r.D29</td>}
                                @if (r.D30 != null) {<td>@r.D30</td>}
                                @if (r.D31 != null) {<td>@r.D31</td>}

    Saturday, July 11, 2015 8:43 PM
  • User-166373564 posted

    Hi,

    Try this code snippet:

     @for (int j = 1; j <= days; j++)
               {
                @ {string duty = "D" + j;}
                 <td>
                     @r.duty
                 </td>
               }
    

    With regards,

    Angie

    Tuesday, July 14, 2015 5:11 AM
  • User325035487 posted

    Parser Error Message: Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.

    The first error was the space between @ and { in the line

    @ {string duty = "D" + j;}

    So.. still i cant have variable column names looped through despite being in a series.

    Thursday, July 23, 2015 1:30 AM
  • User-821857111 posted
    @foreach (var r in data)
    {
    <tr>
    
          @for (int j = 1; j <= days; j++)
          {
              <td>@r["D" + j]</td>
          }
    </tr>
    }

    You can use an indexer to access dynamic properties instead of dot notation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 23, 2015 1:57 AM
  • User325035487 posted

    Thank you so much. I can delete hundreds of lines of repeated code with this.

    Friday, July 24, 2015 6:34 AM