Solved

Trim text in gridview column

Posted on 2008-10-02
2
1,896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
jvulliet earned 500 total points
ID: 22625132
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
ID: 31502410
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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