Click here to Skip to main content
11,428,497 members (66,199 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I show all data on grid view on the button click and I enable padding in grid view data is show on grid view but when i click on grid padding (like 12345) then after value is not show on grid view.
What should i do now?

I use these code :
<div>
   <table class="style1">
           <tr><td class="style4" width="156px">Find a Class</td>
               <td align="center" class="style5">
                   <asp:DropDownList ID="DropDownList2" runat="server">
                       <asp:ListItem>Select Class</asp:ListItem>
                       <asp:ListItem>Language Class English</asp:ListItem>
                       <asp:ListItem>Music Class</asp:ListItem>
                       <asp:ListItem>Baby Sitters</asp:ListItem>
                       <asp:ListItem>Skating Classes</asp:ListItem>
                       <asp:ListItem>Yoga Class</asp:ListItem>
                       <asp:ListItem>Swimming Class</asp:ListItem>
                       <asp:ListItem>Chess Class</asp:ListItem>
                       <asp:ListItem>Self Defence Classes</asp:ListItem>
                       <asp:ListItem>Tennis Classes</asp:ListItem>
                       <asp:ListItem>Personality Development For Children</asp:ListItem>
                       <asp:ListItem>Martial Art Training Centres</asp:ListItem>
                       <asp:ListItem>Karate Classes</asp:ListItem>
                       <asp:ListItem>Cricket Coaching Classes</asp:ListItem>
                       <asp:ListItem>Schools</asp:ListItem>
                       <asp:ListItem>Computer Training Institutes</asp:ListItem>
                       <asp:ListItem>Aerobic Classes</asp:ListItem>
                       <asp:ListItem>Badminton Court</asp:ListItem>
                       <asp:ListItem>Kick Boxing Classes</asp:ListItem>
                       <asp:ListItem>Abacus Classes</asp:ListItem>
                       <asp:ListItem>Kindergartens</asp:ListItem>
                       <asp:ListItem>Hobby Classes</asp:ListItem>
                       <asp:ListItem>Kung FU Classes</asp:ListItem>
                       <asp:ListItem>Taekwondo Class</asp:ListItem>
                       <asp:ListItem>Summer Camps</asp:ListItem>
                       <asp:ListItem>Dance Classes</asp:ListItem>
                   </asp:DropDownList>
               </td><td align="center" width="100px">
 
                       <asp:DropDownList ID="DropDownList3" runat="server"
                           >
                           <asp:ListItem>Noida</asp:ListItem>
                           <asp:ListItem>Indirapuram</asp:ListItem>
                           <asp:ListItem>Vaishali</asp:ListItem>
                           <asp:ListItem>Vasundhara</asp:ListItem>
                           <asp:ListItem>Sahibabad</asp:ListItem>
                           <asp:ListItem>Kaushambi</asp:ListItem>
                       </asp:DropDownList>
 
               </td>
               <td align="center" class="style2" width="100px">
                   <asp:Button ID="Button1" runat="server"  Text="GO" onclick="Button1_Click"
                       Height="31px" Width="71px" BackColor="#B9B9B9" BorderStyle="None" />
                   &nbsp;</td>
           </tr>
          <tr><td class="style4" colspan="4">
              &nbsp;</td>
                   </tr>
           <tr>
               <%--<asp:Panel ID="Panel2" runat="server" Visible="False">
                <td class="style4">
               <asp:Label ID="Label1" runat="server" Text="InSector"></asp:Label>
           </td>
           <td>
 
           </td>
           <td>
 
            </td>
               </asp:Panel>--%>
 
           </tr>
 

           </table>
   </div>
       <asp:GridView ID="GridView1" runat="server" Width="614px" AllowPaging="True"
           onpageindexchanging="GridView1_PageIndexChanging1">
       </asp:GridView>
This is c# code :
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    SqlConnection con;
    SqlCommand cmd;
    DataSet ds;
    string connection;
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=RESTON-PC;Initial Catalog=Easy2Connect;Integrated Security=True");
 
        con.Open();
        string ret = "select * from SearchData where ClassName = '" + DropDownList2.SelectedItem + "'and City = '" + DropDownList3.SelectedItem + "'";
        cmd = new SqlCommand(ret, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
   
    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}
Please help me.
Posted 9-Nov-12 20:52pm
M@anish1.3K
Edited 9-Nov-12 22:35pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ds //<-- This line is missing. There is no datasource being set on Page changing event.
        GridView1.DataBind();
    }

You can hold the data source in a variable so that you don't have to duplicate code to re populate the data source:

private DataSet GridView1DataSource
{
  get
  {
    if(Session["gridview1datasource"] == null)
    {
      Session["gridview1datasource"] = new DataSet;
    }
    return (DataSet)Session["gridview1datasource"];
  }
  set
  {
    Session["gridview1datasource"] = value;
  }
}

Then change the Button1_Click to be as follows:
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=RESTON-PC;Initial Catalog=Easy2Connect;Integrated Security=True");
 
        con.Open();
        string ret = "select * from SearchData where ClassName = '" + DropDownList2.SelectedItem + "'and City = '" + DropDownList3.SelectedItem + "'";
        cmd = new SqlCommand(ret, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        //DataSet ds = new DataSet(); <-- Don't need this line now as the DS is created by the GridView1DataSource property.
        da.Fill(GridView1DataSource); //<-- change this to use the session variable.
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }

All the best.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100