Sunday, June 21, 2015 Show limited characters in gridview column

Introduction: In this article will explain how to show limited characters in gridview column using

I have a table Tb_Hotel :
Create table Tb_Hotel
HotelID int not null identity,
HotelName varchar(100),
HotelDescription varchar(max),
Hotelimg varchar(max)
I want to show hotels detail in Gridview data control. Hotel description is too long so want to show the limited charaters (short description) in description column of Gridview. To implement the functionality I create the store procedure:

Create proc Sp_GetHotelDetail
As begin
Select HotelID, HotelName,Hotelimg,left(HotelDescription,25)+'...' Shortdecription from dbo.Tb_Hotel

Only 25 charters will be displayed in column.

Now add a webform to project/website. drag and drop the Gridview from toolbox to webform.

HTML markup of page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
          <asp:TemplateField HeaderText="Hotel Name">
        <asp:Label ID="lblname" runat="server" Text='<%# Eval("HotelName") %>'></asp:Label>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Hotelimg") %>' Width="250"/>
        <asp:TemplateField HeaderText="Description">
       <asp:Label id="lbldescription" runat="server" Text='<%# Eval("Shortdecription") %>'></asp:Label>
             <asp:HyperLink ID="hlRead" runat="server" Font-Bold="True" Font-Size="Small"
Font-Underline="True" ForeColor="#0099FF"
NavigateUrl='<%# Eval("HotelID","detail.aspx?id={0}") %>'>Read More</asp:HyperLink>

After that write the code to bind the gridview.

Firstly add the namespaces.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Create a sqlconnetion.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())

Write the code to bind the gridview.
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    public void BindDatalist()
            SqlDataAdapter adp = new SqlDataAdapter("Sp_GetHotelDetail", con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (dt.Rows.Count > 0)
                GridView1.DataSource = dt;
        catch (Exception ex)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Public Sub BindDatalist()
            Dim adp As New SqlDataAdapter("Sp_GetHotelDetail", con)
            adp.SelectCommand.CommandType = CommandType.StoredProcedure
            Dim dt As New DataTable()
            If dt.Rows.Count > 0 Then
                GridView1.DataSource = dt
            End If
        Catch ex As Exception
        End Try
    End Sub

Now build, run the project and see the result.
Result: Show limited characters in gridview column

In this article we have learn how to display limited characters in gridview column in (C#,VB). I hope you enjoyed this article.

No comments:

Post a Comment