Introduction: In this
article will explain how to show limited characters in gridview column using
asp.net.
Implementation:
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
End
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">
<Columns>
<asp:TemplateField HeaderText="Hotel Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("HotelName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Hotelimg") %>' Width="250"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<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>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
After
that write the code to bind the gridview.
Firstly
add the namespaces.
C#:
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
VB:
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.Configuration
Create
a sqlconnetion.
C#:
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
VB:
Private
con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
Write
the code to bind the gridview.
C#:
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindDatalist();
}
}
public void BindDatalist()
{
try
{
SqlDataAdapter
adp = new SqlDataAdapter("Sp_GetHotelDetail", con);
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable
dt = new DataTable();
adp.Fill(dt);
if
(dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch (Exception ex)
{
}
}
VB:
Protected Sub Page_Load(sender As
Object, e As
System.EventArgs) Handles
Me.Load
If Not IsPostBack Then
BindDatalist()
End If
End Sub
Public Sub BindDatalist()
Try
Dim
adp As New SqlDataAdapter("Sp_GetHotelDetail",
con)
adp.SelectCommand.CommandType = CommandType.StoredProcedure
Dim
dt As New DataTable()
adp.Fill(dt)
If
dt.Rows.Count > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
End
If
Catch
ex As Exception
End Try
End Sub
Now
build, run the project and see the result.
Result:
In this article we have learn how to display limited characters in gridview column in asp.net (C#,VB). I hope you enjoyed this article.
No comments:
Post a Comment