Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6554
  • Last Modified:

ASP.NET Grid View: Display data with line breaks

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
JANWIL78
Asked:
JANWIL78
  • 6
  • 4
1 Solution
 
daveamourCommented:
0
 
JANWIL78Author Commented:
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
 
daveamourCommented:
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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
JANWIL78Author Commented:
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
 
daveamourCommented:
Youre welcome and thanks for the points
0
 
daveamourCommented:
Interestingly I worked on a golf scoring system on my last contract
0
 
JANWIL78Author Commented:
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
 
daveamourCommented:
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
 
JANWIL78Author Commented:
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
 
daveamourCommented:
Yep its all very tough - I do front, middle and backend so a bit of a jack of all trades!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now