Solved

Trim text in gridview column

Posted on 2008-10-02
2
1,846 Views
Last Modified: 2013-12-17
Is it possible to do a trim the text in a gridview to only show the first 10 characters.
I can do this in sql, but i really need to be able to trim the text once in the gridview.

0
Comment
Question by:Mr_Shaw
2 Comments
 
LVL 2

Accepted Solution

by:
jvulliet earned 500 total points
Comment Utility
Hello,

for this purpose you can create a Template field with a Label in it, then use the RowDataBound event in the code behind to set the Text property ( I used a datarowview as dataitem, which is if you Bind your GridView with a DataSet).

See the code snippet.

Thanks
ASP.NET Code:   
 

<asp:gridview ID="gridview" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridview_RowDataBound">

    <Columns>

        <asp:TemplateField  HeaderText="Field">

            <ItemTemplate>

                <asp:Label ID="lblfield" runat="server" ></asp:Label>

            </ItemTemplate>

        </asp:TemplateField>

    </Columns>

 </asp:gridview>
 

Code Behind:
 

    protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            DataRowView drv = (DataRowView)e.Row.DataItem;

            Label lblfield = (Label)e.Row.FindControl("lblfield");

            lblfield.Text = drv["MyField"].ToString().Substring(0, 10);

        }

    }

Open in new window

0
 

Author Closing Comment

by:Mr_Shaw
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now