1

How do I handle null value for columns in the web grid?

I am using the webgrid for displaying the records in a webgrid using MVC3 asp.NET Grid.

  var grid = new WebGrid(source: Model,selectionFieldName:"SelectedRow",rowsPerPage: 10, canPage: true, canSort: true, defaultSort: "Absentee.Name");
     @grid.GetHtml(grid.Column("AbsEnd", "AbsEnd"))

If the AbsEnd is Null. How do I handle this to display a custom string in that column. Like value does not exist.

2 Answers 2

5

Sorry not to Mislead you: but this is the way to get it working:

 grid.Column("Absend", format: (item)=> string.IsNullOrEmpty(item.AbsEnd)?string.Empty:item.AbsEnd),

Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

Your solution depends on the setup of you model. For example:

 public class MyModel
    {
        [Key]
        [DataMember]
        public Guid Id { get; set; }
        [DataMember]
        public string Containername { get; set; }
        [DataMember]
        public Guid? ChildID { get; set; }
        [DataMember]
        public int? OtherListId { get; set; }
        public virtual ChildModel Child { get; set; }
        public virtual ICollection<OtherList> otherList { get; set; }
}

Then your grid would need to be setup as follows:

@vGrid.GetHtml(columns: vGrid.Columns(
   vGrid.Column("ID", header: "ID"),
   vGrid.Column("Child", header: "Raw Drive Label",
      format: (item) => item.Child == null ? new MvcHtmlString("") : item.Child.ID),
   vGrid.Column("otherList", header: "otherList", format: (item) =>
   {
      WebGrid subGrid = new WebGrid(source: item.otherList);
      return subGrid.GetHtml(
       htmlAttributes: new { id = "subT" },
       columns: subGrid.Columns(subGrid.Column("Name")
      )
      );
      })
))

Since "otherList" is an ICollection, or any type of list, you have to loop through it to display the data.

Add the following style to get rid of the table formatting in the internal table, to make it look like part of the external table:

#subT {
        border: none;
        padding: 0;
        margin: 0;
    }
        /*don't show the table header:*/
        #subT > thead {
            display: none;
        }
        #subT > tbody td{
            border: none;
        }

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.