Monday 22 June 2015

GridView - How to replace line breaks in GridView

watch this example on YouTube:




html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewReplaceLineBrakes.aspx.cs" Inherits="WebApplication1.GridViewReplaceLineBrakes" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>sdsad</title>
</head>
<body>
   
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label1"  runat="server" Text='<%# ((string)Eval("Name")).Replace("\n", "<br />") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class GridViewReplaceLineBrakes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = LoadData();
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        public DataTable LoadData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Rows.Add(new object[] { "Frank\nSomething" });
            dt.Rows.Add(new object[] { "Bob\nSomething Else" });
            dt.Rows.Add(new object[] { "Ann\nLast Name" });
            return dt;
        }
    }
}

1 comment:

  1. hi, you created very helpfull clips, good work! im impressed about your mvc skills and motivation. Are you doing these clips just for fun or do you earn money with? I am looking for a honest and skilled co/partner for a special .Net MVC programming and business project. May i know about your current profession/job? Best regards, Tom

    ReplyDelete