Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trim text in gridview column

Posted on 2008-10-02
2
Medium Priority
?
1,928 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 1500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

783 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