MarkUp:

 asp:TextBox>asp:Button CssClass="SubmitButton" ID="btnView" runat="server" Text="View List" OnClick="btnView_Click" />

 

   

 


 

 

 

Codebehind:

  void btnView_Click(object sender, EventArgs e)
 {
    PopulateGridView();
 }

   public void PopulateGridView()
    {
        try
        {
            SqlConnection sqlCon = new SqlConnection(strOnyxConnectionString);
            SqlCommand sqlCmd = new SqlCommand("SProcName", sqlCon);
            sqlCmd.CommandType = CommandType.StoredProcedure;

            sqlCmd.Parameters.Add("@Param1", SqlDbType.VarChar);
            sqlCmd.Parameters["@Param1"].Value = txtBulletinName.Text;

           
            EmailListDS = new DataSet();
            SqlDataAdapter emailAdapter = new SqlDataAdapter(sqlCmd);
            emailAdapter.Fill(EmailListDS, "Tbl_EmailList");

            if (EmailListDS.Tables["Tbl_EmailList"] != null )
            {
                DataTable dt = EmailListDS.Tables["Tbl_EmailList"] as DataTable;
                if (dt != null)
                {
                    gvEmailList.Visible = true;
                    lblPaging.Visible = true;
                    DataView dv = new DataView(dt);
                    dv.Sort = "LastName ASC";
                    gvEmailList.DataSource = dv;
                    gvEmailList.DataBind();
                    lblPaging.Text = "You are Viewing Page " + Convert.ToInt32(gvEmailList.PageIndex + 1) + " of " + gvEmailList.PageCount + " for " + drpEmailFormat.SelectedItem.Text;
                }
            }
            else
            {
                gvEmailList.Visible = false;
                lblPaging.Visible = false;
                lblMessage.Text = "No Records found";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.ToString();
        }
    }

 
//Method that sorts data

 protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
    {

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            if (GridViewSortExpression != string.Empty)
            {
                if (isPageIndexChanging)
                {
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
                }
                else
                {
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
                }
            }
            else
            {
                dataView.Sort = string.Format("{0} {1}", "LastName", "ASC");
            }

            return dataView;
        }
        else
        {
            return new DataView();
        }

    }

 

 private string GridViewSortDirection
    {
        get { return ViewState["SortDirection"] as string ?? "ASC"; }

        set { ViewState["SortDirection"] = value; }
    }

    private string GetSortDirection()
    {
        switch (GridViewSortDirection)
        {
            case "ASC":
                GridViewSortDirection = "DESC";
                break;

            case "DESC":
                GridViewSortDirection = "ASC";
                break;
        }
        return GridViewSortDirection;
    }

    private string GridViewSortExpression
    {
        get { return ViewState["SortExpression"] as string ?? string.Empty; }
        set { ViewState["SortExpression"] = value; }
    }

 

protected void gvEmailList_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            GridViewSortExpression = e.SortExpression;
            int pageIndex = gvEmailList.PageIndex;
            gvEmailList.DataSource = SortDataTable(EmailListDS.Tables[0] as DataTable, false);
            gvEmailList.DataBind();
            gvEmailList.PageIndex = pageIndex;
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.ToString();
        }
    }

protected void gvEmailList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvEmailList.DataSource = SortDataTable(EmailListDS.Tables[0] as DataTable, true);
        gvEmailList.PageIndex = e.NewPageIndex;
        gvEmailList.DataBind();
        lblPaging.Text = "You are Viewing Page " + Convert.ToInt32(gvEmailList.PageIndex + 1) + " of " + gvEmailList.PageCount + " for " + drpEmailFormat.SelectedItem.Text;

    }