Search code examples
gridviewsqldatasource

How to Enforce Gridview Line Breaks


I am trying to use an sqldatasource object to populate a grid view but I wish to combined two of the rows in the database into one row with a line break in the grid view (for aesthetic appeal only). What I have done so far is concatenated the results of the database to look something like this:

SELECT Incidents.Title + '<br />' + Products.Name AS "Title/Product",
Technicians.Name + '<br />' +  Incidents.Description AS "Tech name /<br />Description",
Incidents.DateOpened,
Incidents.DateClosed
FROM Incidents
INNER JOIN Technicians ON Incidents.TechID = Technicians.TechID
INNER JOIN Products ON Incidents.ProductCode = Products.ProductCode
WHERE Incidents.CustomerID = @CustomerID

So as you can see I am inserting HTML linebreaks into the contatination and the column names but sadly this is shown as "Title<br />Product" in the gridview. In short it does not actually perform the linebreak it just inserts the text.

So my question is, is there anyway to enforce a linebreak, preferably one that is less complex then the method I am working with, if there isn't that's fine I'm just really stuck on exactly what I could do.


Solution

  • Yep you don't get a line break when you insert it in your SQL query. You may also look into using a ListView control. The asp:ListView Control

    You can do everything regarding aesthetics inside your presentation layer (aspx page).

    For your requirement, what you can do is to create a gridview template field column. Inside that template field column you can drop some labels & place your line breaks there. You can bind your labels with your underlying data source like

    <asp:TemplateField HeaderText="My line break row">
    <ItemTemplate>
    <p>
      <asp:Label Id="lblTitle" Text='<%# Bind("Title")></asp:Label>
    </p>
    <p>
      <asp:Label Id="lblProduct" Text='<%# Bind("Product")></asp:Label>
    </p>
    
    </ItemTemplate>
    </TemplateField>