Set the values to Null if its a 0 on Data Grid asp.net

314 views Asked by At

I have a Data grid that has Item Templates with Labels in it:

       <asp:datagrid id="ID" runat="server" Width="641px" CellPadding="2" PageSize="2"  DataKeyField="IDs"
                            AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="ID_ItemDataBound" >
      <SelectedItemStyle ForeColor="HighlightText" BackColor="Highlight">   </SelectedItemStyle>
           <AlternatingItemStyle BackColor="WhiteSmoke">
        </AlternatingItemStyle>
                            <HeaderStyle Font-Bold="True" BackColor="AliceBlue">
        </HeaderStyle>
                            <FooterStyle Font-Bold="True" BackColor="AliceBlue">
        </FooterStyle>
                            <Columns>
            <ItemTemplate>
<asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="dataScore" Text='<%# DataBinder.Eval(Container, "DataItem.dataScore" ) %>'>
     </asp:label>
    <asp:label BorderStyle=None Text='<%# GetCompleteIncomplete(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.dataScore")!=null)) %>' Visible='<%# DataBinder.Eval(Container, "DataItem.IsCompleteOrNot") %>' id="txtIsComplete" runat="server">
        </asp:label>
            </ItemTemplate>
            </columns>

And I am trying to Set the 0s to blank instead of showing 0 in the DataGrid so on ItemData bound I get the Label like this and trying to set the value to null:

if ((e.Item.ItemType == ListItemType.Item) ||
          (e.Item.ItemType == ListItemType.AlternatingItem))
        {

            Label dataScore = (Label)e.Item.FindControl("dataScore"); // Gets that Label
            Label txtIsComplete = (Label)e.Item.FindControl("txtIsComplete");



 if(dataScore .Text == "0")
            {
                dataScore.Text = string.Empty; // Tried 
            }
1

There are 1 answers

5
Anderson Silva On

You can write a function to analyse the value of DataBinder.Eval

Private String MyFunction(String value)
{
    Return value == "0" ? String.Empty : value;
}

<asp:label BorderStyle=None Visible='<%# ReverseBool(Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.IsCompleteOrNot"))) %>'   runat="server" ID="dataScore" Text='<%# MyFunction(DataBinder.Eval(Container, "DataItem.dataScore" )) %>'>