Replace null value databind item with default value

Here’s  the code to set default value to gridview/datalist itemtemplate if it is bound with null value or the dataset returns null value.

 

<asp:TemplateField HeaderText="Header">

                <ItemTemplate>

                <asp:Label ID="lbl_amt" runat="server"  Text='<%# string.Format("{0}",string.IsNullOrEmpty(Eval("YourDataColumn").ToString())?"NA":Eval("YourDataColumn")) %>'></asp:Label>

                </ItemTemplate>

                </asp:TemplateField>

 

The above Code results in NA if YourdataColumn is returned as null. Writing code in Rowdatabound to display “NA” for null values will be avoided with this.

No Comments