Solved

ASP.NET Grid View: Display data with line breaks

Posted on 2011-03-10
10
5,505 Views
Last Modified: 2012-05-11
I am using an asp:gridview to display a golf lesson schedule.  I have three columns in the gridview (see image gridview.png) -  Series (a concatenation of the series name, target golfer level, description), Dates (concatenation of Weekday with time, can have multiple dates for the series), Location/price (course location / price).  I am retrieving the data from a database and formatting the results into strings. Each of the strings are dynamic.  There can be one class or there can be 5 classes.  

strSeriesSummary = "Swing Tune Up Series~(for golfers with some experience)~One and a half hours, covering short game and full swing techniques"

strDateTimeSummary = "Saturday, 10:00 AM - 11:30 AM~ - Chipping & Full Swing  Mar 12~ - Pitching & Full Swing  Mar 19~ - Sand & Full Swing  Apr 2"

strLocationPriceSummary= "Red Wing Lake Golf Course~$25.00 per session"

I am trying to get a VBCRLF where the tilde (~) is in the string.  Research indicates that asp.net does not like VBCRLF or any CRLF.
- I have tried replacing the CRLF with <br/>  - the result is that the <br/> is displayed as part of the string - as in "Red Wing Lake Golf Course<br/>$25.00 per session"
- I have tried using Chr(13) & Chr(10) - no luck - translates to spaces
- I have tried code in the DataBound method of the gridview to replace the "~" with VBCRLF
gvRow.Cells(cellCount).Text = gvRow.Cells(cellCount).Text.Replace("~", vbCrLf)

- I have tried placing the <pre> tag in front of the grid - this, combined with the code in the gridview DataBound method, accomplishes the desired format but inserts a huge gap before the grid; I have researched getting rid of that gap but the results did not work for me.

- I have tried a templatefield with no luck - but perhaps I did it incorrectly

I have included the gridview code here, an image of what the grid looks like currently (gridview.png) and an image of what I am trying to accomplish.

I have spent too much time on this and have seen other posts in different resources regarding the issue of line breaks in grid views but have not seen a solid solution or one that addresses this particular scenario.

I will award points for an actual solution and for assistance.

<asp:GridView 
                                        ID="gvResults" 
                                        runat="server" 
                                        AutoGenerateColumns="False" 
                                        AllowPaging="True" 
                                        PageSize="2"
                                        Font-Size="X-Small" 
                                        Width="530px" 
                                        HorizontalAlign="Left" ForeColor="#000066"
                                         AlternatingRowStyle-ForeColor ="#000066"    >
                                        <Columns>
                                           <asp:BoundField DataField="ID" ReadOnly="True" HeaderText="ID" Visible="False"  ></asp:BoundField>
                                           <asp:BoundField DataField="SeriesSummary" ReadOnly="True" HeaderText="Series" Visible="True"  ItemStyle-Wrap="true" ItemStyle-Width="170px">
                                            </asp:BoundField>
                                           <asp:BoundField DataField="DayDateSummary" ReadOnly="True" HeaderText="Dates" Visible="True"  ItemStyle-Width="210px" ></asp:BoundField>
                                           <asp:BoundField DataField="LocationPriceSummary" ReadOnly="True" HeaderText="Location/Price"  ItemStyle-Width="150px"  ></asp:BoundField>      
                                        </Columns>
                                        <RowStyle  Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" HorizontalAlign="Justify" BackColor="#BCF989" Height="18px"  ForeColor="#242AAD" ></RowStyle>
                                        <AlternatingRowStyle BackColor="White"  CssClass="txtLessonLarge"    ForeColor="#000066"/>
                                        <HeaderStyle BackColor="#42B617" Font-Bold="True" Font-Size="10pt" HorizontalAlign="Left" />
                                        <PagerStyle Font-Names="Arial" Font-Size="X-Small" HorizontalAlign="Center" />
                                    </asp:GridView>

Open in new window

GridView.png
DesiredResult.png
0
Comment
Question by:JANWIL78
[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
  • 6
  • 4
10 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 35106617
0
 

Author Comment

by:JANWIL78
ID: 35108729
Thank you for your assistance daveamour...I tried this and it didn't work.  I've added the gridview html.  The gridhtmlencode-false image shows the results.  I added the <pre> tag just before the gridview.  It formats correctly but also adds a huge gap before the grid.  See the gridwithpreandcss image.
<asp:GridView 
                                        ID="gvResults" 
                                        runat="server" 
                                        AutoGenerateColumns="False" 
                                        AllowPaging="True" 
                                        PageSize="2"
                                        Font-Size="X-Small" 
                                        Width="530px" 
                                        HorizontalAlign="Left" ForeColor="#000066"
                                         AlternatingRowStyle-ForeColor ="#000066"    >
                                        <Columns>
                                           <asp:BoundField DataField="ID" ReadOnly="True" HeaderText="ID" Visible="False"  ></asp:BoundField>
                                           <asp:BoundField DataField="SeriesSummary" ReadOnly="True" HeaderText="Series" Visible="True"  ItemStyle-Wrap="true" ItemStyle-Width="170px" HtmlEncode="false">
                                            </asp:BoundField>
                                           <asp:BoundField DataField="DayDateSummary" ReadOnly="True" HeaderText="Dates" Visible="True"  ItemStyle-Width="210px"  HtmlEncode="false"></asp:BoundField>
                                           <asp:BoundField DataField="LocationPriceSummary" ReadOnly="True" HeaderText="Location/Price"  ItemStyle-Width="150px"  HtmlEncode="false" ></asp:BoundField>      
                                        </Columns>
                                        <RowStyle  Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" HorizontalAlign="Justify" BackColor="#BCF989" Height="18px"  ForeColor="#242AAD" ></RowStyle>
                                        <AlternatingRowStyle BackColor="White"  CssClass="txtLessonLarge"    ForeColor="#000066"/>
                                        <HeaderStyle BackColor="#42B617" Font-Bold="True" Font-Size="10pt" HorizontalAlign="Left" />
                                        <PagerStyle Font-Names="Arial" Font-Size="X-Small" HorizontalAlign="Center" />
                                    </asp:GridView>

Open in new window

gridhtmlencode-false.png
GridWithPreAndCss.png
0
 
LVL 19

Accepted Solution

by:
daveamour earned 500 total points
ID: 35108962
It works fine for me - see code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Grid Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="PeopleGrid" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="Firstname" HeaderText="Firstname" />
                <asp:BoundField DataField="Surname" HeaderText="Surname" />
                <asp:BoundField DataField="Address" HeaderText="Address" HtmlEncode="False" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace GridTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PeopleGrid.DataSource = GetPeople();
            PeopleGrid.DataBind();
        }

        public List<Person> GetPeople()
        {
            List<Person> people = new List<Person>();

            people.Add(new Person { Firstname = "Dave", Surname = "Amour", Address = "14 Smith Road<br />Mansfield<br />Notts" });
            people.Add(new Person { Firstname = "Fred", Surname = "Smith", Address = "15 Smith Road<br />Mansfield<br />Notts" });
            people.Add(new Person { Firstname = "Sarah", Surname = "Brown", Address = "16 Smith Road<br />Mansfield<br />Notts" });
            people.Add(new Person { Firstname = "Tracey", Surname = "Green", Address = "17 Smith Road<br />Mansfield<br />Notts" });

            return people;
        }
    }

    public class Person
    {
        public string Firstname { get; set; }
        public string Surname { get; set; }
        public string Address { get; set; }
    }
}

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:JANWIL78
ID: 35109108
Oh my gosh, daveamour, I am so sorry!  I only tried half of your solution.  I overlooked the part about the <br/> tag.  Once I put the tag in my string and with the htmlencode=false, it worked.    Thank you so much!
0
 
LVL 19

Expert Comment

by:daveamour
ID: 35109158
Youre welcome and thanks for the points
0
 
LVL 19

Expert Comment

by:daveamour
ID: 35109175
Interestingly I worked on a golf scoring system on my last contract
0
 

Author Comment

by:JANWIL78
ID: 35109485
I feel like a dweeb for not catching the full solution the first time.  I read your piece but when I looked at the referenced post I lost the <br/> part.  I had tried the <br/> tag before but didn't combine it with the htmlencoude=false.  I did try to convince the client to just do straight grid rows but this look is preferred because the lessons are in series.  This is the format that is distributed on printed schedules.  Anyway there are a couple tweeks still needed to the look but this was the biggest hurdle.  I picked up golf a couple years ago.  It's great to get outdoors!   Thank you again!
0
 
LVL 19

Expert Comment

by:daveamour
ID: 35109597
I don't actually play golf but have played a lot of computer golf in the past so knew how all the scoring works.

I come from a classic ASP background before asp.net so it hellps to understand whats really going on under the hood.
0
 

Author Comment

by:JANWIL78
ID: 35110245
I prefer coding the backend and databases.  I don't like the look and feel stuff ...kills me.... that is definitley why I had such a hard time getting that grid/format to work.  It does make a difference when building an application if you know or are at least familiar with the intricacies of the situation.  Part of the battle is understanding the method to the madness.  :-)
0
 
LVL 19

Expert Comment

by:daveamour
ID: 35114713
Yep its all very tough - I do front, middle and backend so a bit of a jack of all trades!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

739 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