String Format Specifier in C# for with fixed length of strings in Gridview

817 views Asked by At

I am trying to display string with fixed length (say 10 Digits of strings) in Grid view item template while binding, i could not find any Format specifiers for the string itself.

i can get format specifiers for (Numbers{0:N}, Floats & Decimal (D), Currency{0:C}, , Date{1,8:yyyy} , Percentage {0,3:P1},Temperature: {0:F}, Exponential, Hexadecimal ... But Not For String Itself)

I tried links: Click here but didn't work for me.

My Grid view have an Template Field (Item Template)

<asp:TemplateField HeaderText="Notes">
     <ItemTemplate>

     <asp:Label ID="Label_Note" runat="server" Text='<%# String.Format("{0}", Eval("Defect_Note").ToString()) %>'  ></asp:Label>   

    </ItemTemplate>
 </asp:TemplateField>

I need to display 'Notes Column' in below grid with max 10 digits. if exeeds it should not show (can show on tooltip) if less than 10 it can show all its content.

enter image description here

I wanted to display the Notes columns like Printf(" %8s" ,&note) in C#. (in Binding single line)

2

There are 2 answers

0
hazimdikenli On BEST ANSWER

Don't forget to check for null values.

notes.Substring(0, Math.Min(notes.Length, 10))
2
senthilkumar2185 On

below code help with you

<asp:Label ID="lblDescription" runat="server"
                Text='<%# Limit(Eval("Description"),10) %>' 
                Tooltip='<%# Eval("Description") %>'>
      </asp:Label>

Refer this link:

How to limit label string length in GridView with Read More link?