Paging in DataGrid in alphabetical manner


This article focus on displaying the paging in alphabetical manner using Linkbuttons at the footer of the datagrid. The LinkButtons are added in Item created event and commandName and commandArgument properties of the linkbutton are used to identify them.

In this article, we will use Linkbuttons for paging purpose in datagrid. Lets use Alphabets instead of numbering system of data grid paging. We will use the data grid footer for paging purpose.

Linkbutton has properties like Command Name and COmmand Argument. We will use these properties inside the itemcreated event of the data grid control.

The event that is to be raised is tracked in the ItemCommand Event.

Follow the below Steps to achieve this requirement.

Place the Data grid control in the aspx page.


asp:DataGrid ShowFooter="True" OnItemCreated="ItemCreated" OnItemCommand="ItemCommand" id="dgAplhabeticalPaging" runat="server">


bind the data grid on page load.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Put user code to initialize the page here

If Not Page.IsPostBack Then

BindDataGrid("")

End If

End Sub


Sub BindDataGrid(ByVal strvalues As String)

Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
cn = New SqlConnection("Server=localhost;uid=sa;pwd=;database=pubs")
Dim strsql As String = "Select * from TestData"

If strvalues = "" Then
strsql = strsql
Else
strsql = strsql + " where Test_Name like'" + strvalues + "%'"
End If

da = New SqlDataAdapter(strsql, cn)
ds = New DataSet
da.Fill(ds, "Test")

If (ds.Tables(0).Rows.Count = 0) Then
Response.Write("No Data Found")
dgAplhabeticalPaging.DataSource = Nothing
dgAplhabeticalPaging.DataBind()

Else
dgAplhabeticalPaging.DataSource = ds
dgAplhabeticalPaging.DataBind()

End If

End Sub


Now create the links in the item created and item command events. Check whether the item type is footer and then place the linkbuttons inside the footer.


Protected Sub ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Footer Then

e.Item.Cells.Clear()

Dim tc As TableCell = New TableCell

tc.ColumnSpan = 2

e.Item.Cells.Add(tc)

Dim lc As LiteralControl

Dim lb As LinkButton

Dim i As Integer

For i = 65 To 65 + 25

lc = New LiteralControl

lb = New LinkButton

lc.Text = " "

lb.Text = Chr(i)

lb.CommandName = "Alphabets"

lb.CommandArgument = Chr(i)

tc.Controls.Add(lb)

tc.Controls.Add(lc)

Next

End If

End Sub


Protected Sub ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

If (e.CommandName = "Alphabets") Then
BindDataGrid(e.CommandArgument.ToString())

End If

End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: