Display data from database in ASP.Net GridView with Line Break using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hi I am using below code to store the inforamtion in database column.

string IDate = dtIDate.SelectedDate.ToString("dd-MM-yyyy");
string ISComments = "Interview Date: " + IDate + " <br /> Interview Time: " + txtInterviewTime.Text + "<br /> " + txtComments.Text;

cmd.Parameters.AddWithValue("@Comments", ISComments);

and displaying this database column in grid view.

but line break <br /> are not doing line break they are displaying as text.

I want to show the data in grid like in 3 lines

Interview Date

Interview time

and comments will come in last line.

Any idea how to achieve this.

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi jqdesigner,

Refer below sample.

HTML

Date :
<asp:TextBox runat="server" ID="txtDate" />
<br />
Time :
<asp:TextBox runat="server" ID="txtTime" />
<br />
Comments :
<asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" />
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
<br />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ieComment" HeaderText="Content" HtmlEncode="false" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label Text='<%#Eval("ieComment") %>' runat="server" ID="lblContent" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblInterView", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Insert(object sender, EventArgs e)
{
    string iDate = txtDate.Text;
    string isComment = "Interview Date: " + iDate + " <br /> Interview Time: " + txtTime.Text + "<br /> " + txtComments.Text;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("INSERT INTO tblInterView VALUES(@ieComment)", con);
    cmd.Parameters.AddWithValue("@ieComment", isComment);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    BindGrid();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        (e.Row.FindControl("lblContent") as Label).Text = Server.HtmlDecode(e.Row.Cells[0].Text);
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    BindGrid()
End Sub

Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM tblInterView", con)
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    Dim dt As DataTable = New DataTable()
    da.Fill(dt)
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
    Dim iDate As String = txtDate.Text
    Dim isComment As String = "Interview Date: " & iDate & " <br /> Interview Time: " & txtTime.Text & "<br /> " + txtComments.Text
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblInterView VALUES(@ieComment)", con)
    cmd.Parameters.AddWithValue("@ieComment", isComment)
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()
    BindGrid()
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        TryCast(e.Row.FindControl("lblContent"), Label).Text = Server.HtmlDecode(e.Row.Cells(0).Text)
    End If
End Sub

Screenshot