Home > ASP.Net > Alphabetically List with Gridview

Alphabetically List with Gridview


Hi

try this example to filter Alphabetically

ASPX

<%@ page autoeventwireup="true" codefile="FilterWithAlphabetLinkButtons.aspx.cs"
 inherits="GridView_FilterWithAlphabetLinkButtons" language="C#" masterpagefile="~/MasterPages/Default.master"
 title="GridView: Filter With Alphabet LinkButtons" %>

<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
 <asp:linkbutton id="btnAll" runat="server" text="ALL" onclick="btnAll_Click" />
 <asp:repeater id="rptLetters" runat="server" datasourceid="sdsLetters">
  <headertemplate>
    |
  </headertemplate>
  <itemtemplate>
   <asp:linkbutton id="btnLetter" runat="server" onclick="btnLetter_Click" text='<%#Eval("Letter")%>' />
  </itemtemplate>
  <separatortemplate>
    |
  </separatortemplate>
 </asp:repeater>
 <asp:sqldatasource id="sdsLetters" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
  selectcommand="SELECT DISTINCT LEFT(ProductName, 1) AS [Letter] FROM [Products]">
 </asp:sqldatasource>
 <asp:gridview id="gvProducts" runat="server" datakeynames="ProductID" datasourceid="sdsProducts"
  style="margin-top: 12px;">
 </asp:gridview>
 <asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
  selectcommand="SELECT ProductID, ProductName FROM [Products] ORDER BY [ProductName]">
 </asp:sqldatasource>
</asp:content>

CODE-BEHIND

using System;
using System.Web.UI.WebControls;

public partial class GridView_FilterWithAlphabetLinkButtons : System.Web.UI.Page
{
  protected void btnAll_Click(object sender, EventArgs e)
 {
  gvProducts.DataBind();
 }

 protected void btnLetter_Click(object sender, EventArgs e)
 {
  LinkButton btnLetter = sender as LinkButton;

  if (btnLetter == null) { return; }

  sdsProducts.SelectCommand = String.Format(
   "SELECT ProductID, ProductName FROM [Products] WHERE ([ProductName] LIKE '{0}%') ORDER BY [ProductName]",
   btnLetter.Text);
 }
}

Hope it helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: