Solved

ASP.NET Grid View: Display data with line breaks

Posted on 2011-03-10
10
4,930 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
  • 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

12 Experts available now in Live!

Get 1:1 Help Now