Show only few characteres in a gridview column

Last post 05-19-2008 5:00 AM by nettmester. 13 replies.

Sort Posts:

  • Show only few characteres in a gridview column

    11-02-2007, 7:24 AM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    Hello

    How can i show only 10 characteres from the total of my column text (it's a memo field)

    Thank you 

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    11-02-2007, 7:33 AM
    Answer

    Hi, here is a function I use:

    1    	Public Shared Function ReduceTextLength( _
    2    	 ByVal Text As String, _
    3    	 ByVal Length As Long) As String
    4    
    5    		If Text.Length > Length Then
    6    			Text = Text.Substring(0, Length) & " ..."
    7    		End If
    8    
    9    		Return Text
    10   
    11   	End Function
    
  • Re: Show only few characteres in a gridview column

    11-02-2007, 7:37 AM
    • Loading...
    • somnathmali
    • Joined on 07-16-2007, 8:09 AM
    • Pune , INDIA
    • Posts 257

     1. Use OnRowDataBound Event Of GridView (  you will get simmilar event for other controls also). In that you can use SubString to set only 10 char.

    2. Use TemplateField. And while using DataBinder.Eval ... Call substing directly.
    as
     <asp:TemplateField HeaderText="Memo">
                            <ItemTemplate>
                                <%# (DataBinder.Eval(Container.DataItem, "Memo").ToString()).Substring(5)%>
                            </ItemTemplate>
                        </asp:TemplateField>

     

    But I prefer 1st option as you can handle errors there. 

     

    Somnath Mali
    .NET Developer , Pune INDIA.


    http://www.fotosplatter.com



    http://somnath-ms-dot-net.blogspot.com


    Please Mark As Answer If my reply helped you.

  • Re: Show only few characteres in a gridview column

    11-02-2007, 7:38 AM

     while giving datasource to the GridView; you can write your query in a way that column display only 10 chars.. for ex.,

    select SUBSTRING(FIELD_NAME,0,11) from table

     Good Luck./.
     

    Thanx,
    [KaushaL] || BloG || Profile

  • Re: Show only few characteres in a gridview column

    11-02-2007, 7:39 AM
    • Loading...
    • Careed
    • Joined on 06-24-2002, 7:37 AM
    • Lubbock, TX
    • Posts 774

    I would think that the easiest way would be to pull it from the database that way.  Create a derived field from your database that takes the first 10 characters of your memo field.

    The alternative would be to create a TemplateColumn that would take your memo field and truncate it in the GridView itself.  Regardless of which you use, though, you will not have the whole field available to you from the GridView.  If you plan to edit in the GridView, you will need to use the TemplateField approach and hide the full memo field in the ItemTemplate and show it in the EditItemTemplate, hiding your 10-character text.

    If this isn't the direction you going in, please elaborate further.

     

    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: Show only few characteres in a gridview column

    11-02-2007, 7:53 AM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    Hi there and thank you for your help

    Please how can i in 1.option call substring in OnRowDataBound Event?

    My field name is pedido

    Thank you

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    11-23-2007, 9:11 AM
    Answer
    • Loading...
    • Careed
    • Joined on 06-24-2002, 7:37 AM
    • Lubbock, TX
    • Posts 774

    Let's say that pedido is the third column in the GridView.  Then, try the following:

     

    protected void GridView1_RowDataBound(Object objSender, GridViewEventArgs evtArgs)
    {
       if (evtArgs.Row.RowType == DataControlRowType.DataRow)
       {
          evtArgs.Row.Cells[2].Text = evtArgs.Row.Cells[2].Text.Substring(0, 10);
       }
    }
      
    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: Show only few characteres in a gridview column

    11-26-2007, 6:49 AM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    Careed:

    Let's say that pedido is the third column in the GridView.  Then, try the following:

     

    protected void GridView1_RowDataBound(Object objSender, GridViewEventArgs evtArgs)
    {
       if (evtArgs.Row.RowType == DataControlRowType.DataRow)
       {
          evtArgs.Row.Cells[2].Text = evtArgs.Row.Cells[2].Text.Substring(0, 10);
       }
    }

      

    I need this code in vb.net. I get an error in evtArgs, how can i declare it? (please look at the following code)

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If evtArgs.Row.RowType = DataControlRowType.DataRow Then

    evtArgs.Row.Cells(2).Text = evtArgs.Row.Cells(2).Text.Substring(0, 10)

    End If

    End Sub

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    11-26-2007, 7:26 AM
    • Loading...
    • Careed
    • Joined on 06-24-2002, 7:37 AM
    • Lubbock, TX
    • Posts 774
    In your code, switch evtArgs with e, which is your GridViewRowEventArgs parameter.
    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: Show only few characteres in a gridview column

    11-26-2007, 7:32 AM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    Careed:
    In your code, switch evtArgs with e, which is your GridViewRowEventArgs parameter.

     

    Hi

    got the error

    Reference to a non-shared member requires an object reference.

    By the way and if i have a Template on my gridview. How can i limit the visible number os characteres ?

     

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    11-26-2007, 12:05 PM
    Answer
    • Loading...
    • Careed
    • Joined on 06-24-2002, 7:37 AM
    • Lubbock, TX
    • Posts 774

    If you're using a TemplateField, then you would do everything in the <ItemTemplate>.  Try this: 

    <%# Eval("Memo").ToString().Substring(0, 10) %>
     
    Christopher Reed
    "The oxen are slow, but the earth is patient."
  • Re: Show only few characteres in a gridview column

    05-16-2008, 5:35 PM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    One more problem

    If my memo field is empty i got an error

     Index and length must refer to a location within the string.
    Parameter name: length

    Any help,please?

    Thank you

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    05-16-2008, 6:18 PM
    • Loading...
    • mariolopes
    • Joined on 04-15-2005, 2:42 PM
    • Coimbra - Portugal
    • Posts 470

    Worked the following way

    Text='<%# ReduceTextLength(Eval("Resumo_processo"),10) %>

    in asp.vb

    Public Shared Function ReduceTextLength( _
      ByVal Text As String, _
        ByVal Length As Long) As String   
        If
    Text.Length > Length Then
        Text = Text.Substring(0, Length) & " ..."
        End If
        
        Return
    Text
       
      End Function


    Thank you garrygrimshaw

    Mario Lopes
  • Re: Show only few characteres in a gridview column

    05-19-2008, 5:00 AM
    Answer
    • Loading...
    • nettmester
    • Joined on 04-30-2008, 8:00 AM
    • Posts 17

     U can also do it like following: (If u dont care about the visual. like in administrator pages and so on.)

     

    aspx: (remove the asp:BoundField an insert a asp:TemplateField and EditItemTemplate with a asp:TextBox, and a ItemTemplate with a asp:TextBox, and set the edit-textbox to multiline and the item-textbox to enabled="false".)

     

    <asp:TemplateField HeaderText="Info" ShowHeader="true" SortExpression="Info">
              <EditItemTemplate><asp:TextBox ID="txtTilbudsvareInfoEdit" MaxLength="2000" runat="server" Text='<%# Bind("Annainfo")%>' TextMode="MultiLine"></asp:TextBox></EditItemTemplate>
              <ItemTemplate><asp:TextBox ID="txtTilbudsvareInfoItem" Enabled="false" runat="server" Text='<%# Bind("Annainfo")%>'></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
     
    SMOOTH!  
     
Page 1 of 1 (14 items)