Wednesday, 4 July 2012

ALPHABETICAL PAGING IN GRIDVIEW

ASPX :

  <asp:LinkButton ID="btnAll" runat="server" 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:SQLServerConnectionString %>"
 SelectCommand="SELECT DISTINCT LEFT(emp_name, 1) AS [Letter] FROM [TestApp].[dbo].[JOB_CARD]">

 </asp:SqlDataSource>
  <asp:GridView ID="gvProducts" runat="server" DataKeyNames="emp_name" DataSourceID="SqlDataSource1"  Style="margin-top: 12px;" AutoGenerateColumns="False" PageSize="2">
      <Columns>
               <asp:BoundField DataField="trans_id" HeaderText="idle_cd" ReadOnly="True" SortExpression="trans_id" />
               <asp:BoundField DataField="ticket_no" HeaderText="idle_group" SortExpression="ticket_no" />
                <asp:BoundField DataField="emp_name" HeaderText="idle_desc" SortExpression="emp_name" />
      </Columns>
 </asp:GridView>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLServerConnectionString %>"
                                                SelectCommand=" "></asp:SqlDataSource>
                                        </td>


C# :
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; }
           SqlDataSource1.SelectCommand = String.Format(
             "SELECT [trans_id],[ticket_no],[emp_name] FROM [TestApp].[dbo].[JOB_CARD] where [emp_name] LIKE '{0}%' ORDER BY [emp_name]",
             btnLetter.Text);
        }


 OR

 protected void gvCategories_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                TableCell cell = e.Row.Cells[0];
                cell.ColumnSpan = 2;

                for (int i = 65; i <= (65 + 25); i++)
                {
                    LinkButton lb = new LinkButton();

                    lb.Text = Char.ConvertFromUtf32(i) + " ";
                    lb.CommandArgument = Char.ConvertFromUtf32(i);
                    lb.CommandName = "AlphaPaging";

                    cell.Controls.Add(lb);

                }
            }
        }

        protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AlphaPaging"))
            {
                DataSet ds = new DataSet();
                test1 = new BusinessLayer.Test();
                ds = test1.getDetails(e.CommandArgument.ToString());
                gvEmp.DataSource = ds;
                gvEmp.DataBind();
               
                //gvCategories.DataSource = ds;
                //gvCategories.DataBind();
            }
        }


No comments:

Post a Comment