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

Formatted Label in GridTemplateColumn

2 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
WAMatt
Top achievements
Rank 2
WAMatt asked on 25 Sep 2012, 09:10 PM
Greetings,

I am using the following GridTemplateColumn in a RadGrid...

<telerik:GridTemplateColumn HeaderText="Phone" ItemStyle-Wrap="False">
  <ItemTemplate>
    <asp:Label ID="lblNotifyingIndividualPhoneNumber"
               runat="server"
               Text='<%# string.Format("{0:(###) ###-####}", Convert.ToInt64(Eval("NotifyingIndividualPhone")))%>'>
    </asp:Label>
  </ItemTemplate>
</telerik:GridTemplateColumn>


The problem is when the database field "NotifyingIndividualPhone" is null or empty, it throws an error: "Input string was not in a correct format".  Any ideas how to get around this issue?

Your help is appreciated!
Matt

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Sep 2012, 06:51 AM
Hello,

Try With below code snippet.

Text='<%# Eval("NotifyingIndividualPhone") == null ? string.Empty : string.Format("{0:(###) ###-####}", Convert.ToInt64(Eval("NotifyingIndividualPhone"))) %>'


Thanks,
Jayesh Goyani
0
WAMatt
Top achievements
Rank 2
answered on 26 Sep 2012, 03:16 PM
Thank you, that is a good way to do it all from the markup.  I ended up writing a function so that I could have a bit more flexibility...

ASPX...
<telerik:GridTemplateColumn HeaderText="Phone" ItemStyle-Wrap="False">
  <ItemTemplate>
    <asp:Label ID="lblNotifyingIndividualPhoneNumber"
               runat="server"
               Text='<%# FormatPhoneNumber(Eval("NotifyingIndividualPhone").ToString())%>'>
    </asp:Label>
  </ItemTemplate>
</telerik:GridTemplateColumn>

CP...
protected string FormatPhoneNumber(string s)
{
  if (s == "" || s.Equals(DBNull.Value))
  {
    return "Not found";
  }
  else
  {
    return string.Format("{0:(###)###-####}", Convert.ToInt32(s));
  }
}


Matt
Tags
Grid
Asked by
WAMatt
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
WAMatt
Top achievements
Rank 2
Share this question
or