This is a migrated thread and some comments may be shown as answers.

[Solved] FooterTemplate crash on empty GRID

4 Answers 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lee
Top achievements
Rank 1
Lee asked on 27 Jun 2011, 11:20 AM
I have a simple FooterTemplate on my GRID control that just displays the column total.  When there is data the GRID it all works well however I get an 'Object reference not set to an instance of an object.' error when the GRID is empty.  The model is a IEnumerable.  I'm a unsure as to modify my FooterTemplate code to take an empty GRID into account.  Any help would be gratefully appreciated.

@(Html.Telerik().Grid(Model)
      .Name("risksGrid")
      .Sortable()
      .Filterable()
      .DataKeys(keys => keys.Add(r => r.FrozenFoodsRiskID))
      .ToolBar(commands => commands.Custom().ButtonType(GridButtonType.ImageAndText).Text("Add New Risk").HtmlAttributes(new { @class = "addRisk" }).Action("Create", "FrozenFoodsRisk", new { policyZoneID = ViewData["policyZoneID"], sectionID = ViewData["sectionID"] }))
      .Columns(columns =>
          {
              columns.Bound(r => r.FrozenFoodsRiskID).Title("Edit").Sortable(false).Filterable(false)
                  .Template(r => @Html.ActionLink("Edit", "Edit", "FrozenFoodsRisk",
                  new { riskID = r.FrozenFoodsRiskID, policyZoneID = ViewData["policyZoneID"], sectionID = ViewData["sectionID"] }, new { @class = "editLink" }));
              columns.Bound(r => r.FrozenFoodsRiskID).Title("Delete").Sortable(false).Filterable(false)
                  .Template(r => @Html.ActionLink("Delete", "Delete", "FrozenFoodsRisk",
                  new { riskID = r.FrozenFoodsRiskID, policyZoneID = ViewData["policyZoneID"], sectionID = ViewData["sectionID"] }, new { @class = "deleteLink" }));
              columns.Bound(r => r.SumInsured)
                     .Width(100)
                     .Aggregate(aggregates => aggregates.Sum())
                     .FooterTemplate(@<text>Total: @item.Sum.Format("{0}")</text>);
              columns.Bound(r => r.Rate).Width(100).Format("{0:0.0000}");
              columns.Bound(r => r.Age).Width(50);
              columns.Bound(r => r.Description).Width(200);
              columns.Bound(r => r.InGuaranteePeriod).Width(100);
              columns.Bound(r => r.AgeDiscount).Width(100).Format("{0:0.0000}");
              columns.Bound(r => r.TechnicalPrice)
                     .Width(100)
                     .Aggregate(aggregates => aggregates.Sum())
                     @*.FooterTemplate(@<text>Total: @item.Sum.Format("{0:0.00}")</text>)*@;
          })
    )

Many Thanks,
Lee.

4 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 27 Jun 2011, 11:43 AM
Hello Lee,

Could you please provide a small sample which demonstrates the described error?

All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Lee
Top achievements
Rank 1
answered on 27 Jun 2011, 12:02 PM
I have resolved this by replacing
.FooterTemplate(@<text>Total: @item.Sum.Format("{0}")</text>);
with
.FooterTemplate(@<text>Total: @String.Format("{0}", item.Sum ) </text>);
Strange how you can work for hours without finding an answer and as soon as you post something on a forum you stumble apon the workaround :o)
0
Jerome
Top achievements
Rank 1
answered on 12 Oct 2011, 01:36 AM
Strange you work for hours and stumble upon a forum answer that solves it. Thanks a lot. ;)
By the way, the currency formatting does not work for me. Any idea why?

.FooterTemplate(@<text>@String.Format("{0:c}", item.Sum) </text>);
0
Michel Corbin
Top achievements
Rank 1
answered on 23 Nov 2012, 03:09 PM
First, thank you for your help. In too many times, it's the community that help instead of the telerik member team. :(

When trying what you said with a format currency, the value is not formatted.

I have to do this:

@<text>@string.Format("{0:c0}", item.Sum == null ? 0 : item.Sum.Value)</text>


I have to check if the Sum property is not null because when the grid is empty, this value is null.

I think I will put this statement in an html helper because it's a lot of code mostly when you have a lot of columns with group aggregate and footer aggregate.

Thanx again!

P.S. If anybody have an idea how to put this statement in an html helper, I would be nice.
Tags
Grid
Asked by
Lee
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Lee
Top achievements
Rank 1
Jerome
Top achievements
Rank 1
Michel Corbin
Top achievements
Rank 1
Share this question
or