[Solved] Binding ASP.Net GridView with Paging display Last Page Number on Button Click

Last Reply 7 months ago By arunkurmi

Posted 7 months ago

Hello Sir,

I'm trying to display the gridview data based on dropdown selection, but here the last page is getting displayed, please help me in how to display the first page

<asp:GridView ID="gvAll" runat="server" EmptyDataText="No Record Found" class="table table-bordered table-striped table-hover"
    AutoGenerateColumns="False" OnPageIndexChanging="OnPaging" AllowPaging="True"
    Width="1000px" CellPadding="3" BackColor="White" DataKeyNames="alignmentId" BorderColor="#CCCCCC"
    BorderStyle="None" BorderWidth="1px" RowStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
    PagerSettings-Mode="NumericFirstLast" PageSize="15">
    <Columns>
        <asp:TemplateField>
            <asp:BoundField DataField="alignmentId" HeaderText="S No" ItemStyle-CssClass="hiddencol"
                HeaderStyle-CssClass="hiddencol" />
            <asp:TemplateField HeaderText="S No">
                <itemtemplate>
                                                <%# Container.DataItemIndex + 1 %>
                                            </itemtemplate>
                <itemstyle width="7%" />
            </asp:TemplateField>
            <asp:BoundField HeaderText="Name" DataField="staff_name" SortExpression="staff_name" />
            <asp:BoundField HeaderText="Client Name" DataField="client_name" SortExpression="client_name" />
            <asp:BoundField DataField="day" HeaderText="Day" HeaderStyle-Width="70px" />
    </Columns>
</asp:GridView>

 

public void BindGrid1()
{
    string staff_name = DropDownList1.SelectedItem.Text;
    DateTime from_date = Convert.ToDateTime(txt_fromdate.Text);
    DateTime to_date = Convert.ToDateTime(txt_todate.Text);
    DataTable dt = new DataTable();
    dt = obj_select.select_all_staff_alignment(staff_name, from_date, to_date);
    if (dt.Rows.Count > 0)
    {
        gvAll.DataSource = dt; // give data to GridView
        gvAll.DataBind();
        con.Close();
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "SweetAlert", "swal('Alert!', 'No record found for the given date range!', 'error');", true);
        DropDownList1.ClearSelection();
        txt_todate.Text = "";
        txt_fromdate.Text = "";
    }
}
protected void btn_generate_Click(object sender, EventArgs e)
{
    BindGrid1();
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    if (DropDownList1.SelectedItem.Value == "0" && txt_fromdate.Text == "" && txt_todate.Text == "")
    {
        gvAll.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    else
    {
        gvAll.PageIndex = e.NewPageIndex;
        BindGrid1();
    }
}

 

Posted 7 months ago

Hi democloud,

I have created an sample please refer it.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

Select Country:
<asp:DropDownList runat="server" ID="ddlCountries">
    <asp:ListItem Value="0" Text="--Select--" Selected="True" />
    <asp:ListItem Value="1" Text="USA" />
    <asp:ListItem Value="2" Text="UK" />
</asp:DropDownList>
<br />
<asp:Button Text="Submit" runat="server" ID="btnSubmit" OnClick="Submit_Click" />
<br />
<asp:GridView ID="gvAll" runat="server" EmptyDataText="No Record Found" AutoGenerateColumns="False"
    AllowPaging="True" DataKeyNames="EmployeeID" PagerSettings-Mode="NumericFirstLast"
    PageSize="3" OnPageIndexChanging="gvAll_PageIndexChanging">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="ID" />
        <asp:BoundField DataField="Name" HeaderText="Client Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Namespace

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

VB.NET

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Code

C#

public void BindGrid()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
    if (ddlCountries.SelectedIndex > 0)
    {
        using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,FirstName+' '+LastName Name,City,Country FROM Employees WHERE Country = @Country", con))
        {
            cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text);
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.gvAll.DataSource = ds;
                this.gvAll.DataBind();
            }
        }
    }
}

protected void Submit_Click(object sender, EventArgs e)
{
    this.gvAll.PageIndex = 0;
    this.BindGrid();
}

protected void gvAll_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    this.gvAll.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

VB.NET

Public Sub BindGrid()
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
    If ddlCountries.SelectedIndex > 0 Then
        Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,FirstName+' '+LastName Name,City,Country FROM Employees WHERE Country = @Country", con)
            cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text)
            cmd.CommandType = CommandType.Text
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim ds As DataSet = New DataSet()
                sda.Fill(ds)
                Me.gvAll.DataSource = ds
                Me.gvAll.DataBind()
            End Using
        End Using
    End If
End Sub

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
    Me.gvAll.PageIndex = 0
    Me.BindGrid()
End Sub

Protected Sub gvAll_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    Me.gvAll.PageIndex = e.NewPageIndex
    Me.BindGrid()
End Sub

Screenshot