Searched Datagrid with Paging?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Searched Datagrid with Paging?

    I am new to .NET, so bear with me. I have a datagrid with default
    paging of 10. I can page through it fine to the end. When I do a
    search from a stored procedure the first 10 rows show but when I click
    on 'Next Page' it shows as page 2 from before the search. And when I
    click 'Previous Page' it goes back to page 1 (which is what you get
    when the page loads). Any ideas would be great.

    Thanks,
    Travis

    Travis Guest

  2. Similar Questions and Discussions

    1. Paging a datagrid
      Please, i've created a datagrid that allow numbered paging. But when i click over a page that must be called, i get this error: ...
    2. DataGrid Paging??
      I have code that builds a dataset and connects it to a datagrid. I set up paging but something is broke. I can do a pagecount property and it...
    3. ASP Paging on a searched memo field
      I need advice on how to integrate the paging capbility to my search script. The program I built requires me to make word search on several db...
    4. First Datagrid row with paging
      Hi, My Datagrid header is a shoe size range and depends on the first datagrid row - first article size range. So, i need to get the first row...
    5. using Paging in Datagrid
      Hi, I am adding a paging function to a datagrid control, the datasource is a DataSet Object. Based on some articles, I just add some attributes to...
  3. #2

    Default Re: Searched Datagrid with Paging?

    I can't quite follow what's wrong. So you mean when you introduce searching
    the results from any other page than the first are as if you didn't apply
    the search parameters? So, my question would be how is your DataGrid's OnPageIndexChange
    event coded? It would need to fetch the next 10 records from the search results.
    Perhaps I'm missing part of your question?

    -Brock
    [url]http://staff.develop.com/ballen[/url]

    > I am new to .NET, so bear with me. I have a datagrid with default
    > paging of 10. I can page through it fine to the end. When I do a
    > search from a stored procedure the first 10 rows show but when I click
    > on 'Next Page' it shows as page 2 from before the search. And when I
    > click 'Previous Page' it goes back to page 1 (which is what you get
    > when the page loads). Any ideas would be great.
    >
    > Thanks,
    > Travis


    Brock Allen Guest

  4. #3

    Default Re: Searched Datagrid with Paging?

    I can page fine when the page loads. After I do a search let say my
    stored procedure returns 200 results the first 10 show but when I click
    on next page the it seems as if my search as been reset. It doesn't
    let me see the 190 that are left to page through. Here is my code, if
    that helps:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Globalization;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace GlossaryCreation.Forms
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;
    protected GlossaryCreation.Forms.DataSet3 dataSet31;
    protected System.Data.DataView dataView1;
    protected System.Web.UI.WebControls.Button btnSearch;
    protected System.Web.UI.WebControls.DropDownList dpdlSearch;
    protected System.Web.UI.WebControls.TextBox txtSearch;
    protected System.Web.UI.WebControls.Label lblSearch;
    protected System.Web.UI.WebControls.Button btnAdd;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    DataView Source;

    Source = (DataView)Cache["MyDataView"];

    if(!Page.IsPostBack)
    {
    Page.RegisterHiddenField("__EVENTTARGET", "btnSearch");

    SqlConnection conn = new SqlConnection("workstation id='';packet
    size=4096;user id=id;data source=server;persi" +
    "st security info=True;initial catalog=database;password=pass");
    conn.Open();

    // Create sqlCommand to select username and password from users
    table
    SqlCommand cmd = new SqlCommand("iDBGetHAMSSearchList",conn);

    cmd.CommandType = CommandType.StoredProcedure;

    SqlDataReader dr = cmd.ExecuteReader();

    if(dr.HasRows)
    {
    this.dpdlSearch.DataSource = dr;
    this.dpdlSearch.DataTextField = "column_name";
    this.dpdlSearch.DataValueField = "column_name";
    this.dpdlSearch.DataBind();
    }

    // close dr connection
    dr.Close();
    this.bindSearchResults();
    //this.sqlDataAdapter1.Fill(this.dataSet31);
    //dataView1.Sort = "Serial #";
    //this.DataGrid1.DataBind();
    //Cache["MyDataView"] = dataView1;
    }
    }
    private void bindSearchResults()
    {
    this.dataSet31.Clear();
    this.DataGrid1.DataSource = this.dataSet31;
    this.sqlDataAdapter1.Fill(this.dataSet31);
    this.DataGrid1.DataBind();
    }

    private void dataView1_ListChanged(object sender,
    System.ComponentModel.ListChangedEventArgs e)
    {
    }
    private void DataGrid1_SortCommand(object source,
    DataGridSortCommandEventArgs e)
    {
    dataView1.Sort = e.SortExpression;
    DataGrid1.DataBind();
    }
    public void DataGrid1_PageChanger(object sender,
    System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
    {
    this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
    this.bindSearchResults();
    }

    private void btnSearch_Click(object sender, System.EventArgs e)
    {
    this.DataGrid1.CurrentPageIndex = 0;

    DataView Source;

    Source = (DataView)Cache["MyDataView"];
    if(Page.IsPostBack)
    {
    // Create and open connection to database containing the usernames
    and password
    SqlConnection conn1 = new SqlConnection("workstation id='';packet
    size=4096;user id=id;data source=server;persi" +
    "st security info=True;initial catalog=database;password=pass");
    conn1.Open();

    // Create sqlCommand to select username and password from users
    table
    SqlCommand cmd = conn1.CreateCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "iDBHAMSSearchAsset";

    cmd.Parameters.Add("@ciSearch",SqlDbType.VarChar,5 0);
    cmd.Parameters["@ciSearch"].Value = txtSearch.Text;
    cmd.Parameters.Add("@ciDropList",SqlDbType.VarChar ,15,"column_name");
    cmd.Parameters["@ciDropList"].Value =
    this.dpdlSearch.SelectedValue.ToString();

    if (conn1.State == ConnectionState.Closed)
    conn1.Open();

    this.sqlDataAdapter1.SelectCommand = cmd;

    sqlDataAdapter1.Fill(this.dataSet31);
    this.DataGrid1.DataSource = this.dataSet31;
    DataGrid1.DataBind();


    //close sql conneciton
    conn1.Close();
    }

    this.bindSearchResults();
    }

    Travis Guest

  5. #4

    Default Re: Searched Datagrid with Paging?

    Travis, you will have to devise a way to maintain the state of your
    datasource. You could create the SqlCommand object and store it in the
    viewstate or use some other mechanism. When the CurrentPageIndex is
    changed, you can pull the SqlCommand from ViewState and load the data
    back into the datagrid.

    Jason Bentley
    [url]http://geekswithblogs.net/jbentley[/url]

    Jason Bentley Guest

  6. #5

    Default Re: Searched Datagrid with Paging?

    Ok, I guess my next question is, how exactly do I store the Sqlcommand
    object in a viewstate?

    Travis

    Travis Guest

  7. #6

    Default Re: Searched Datagrid with Paging?

    Here is an example to add the SqlCommand to Session object. The
    ViewState is still an option but you will have to serialize the object
    first. I should have thought of that before I suggested it. Anyway,
    here is enough to get you started.

    To Add The SqlCommand object to the Session.
    Session.Add("SearchCommand", cmd);

    To Retrieve The SqlCommand object from the Session.
    SqlCommand cmd = (SqlCommand)Session["SearchCommand"];

    Jason Bentley
    [url]http://geekswithblogs.net/jbentley[/url]

    Jason Bentley Guest

  8. #7

    Default Re: Searched Datagrid with Paging?

    Jason-

    I was able to add and retrieve the SqlCommand from Session. Thank you.
    I have hopefully one last question, how can I page when the datagrid
    first loads and if I do a search on the same datagrid? Can I do some
    if statement in the pager to check for a search?

    Travis

    Travis Guest

  9. #8

    Default Re: Searched Datagrid with Paging?

    Travis, you CAN easily add the current page index to the ViewState and
    retrieve it. When the page loads:

    YourDataGrid.DataSource = ds;
    if(ViewState["CurrentPageIndex"] != null)
    {
    YourDataGrid.PageIndex =
    Convert.ToInt32(ViewState["CurrentPageIndex"].ToString());
    }
    YourDataGrid.DataBind();

    To save the current page index to the ViewState:
    ViewState.Add("CurrentPageIndex", YourDataGrid.CurrentPageIndex);


    You could also add another variable to the viewstate when the Search
    button is clicked. When you page loads, if the value of the variable is
    null, you know there was no search.

    Jason Bentley
    [url]http://geekswithblogs.net/jbentley[/url]

    Jason Bentley Guest

  10. #9

    Default Re: Searched Datagrid with Paging?

    Jason-

    Sorry about not replying back right away. I was out sick yesterday.
    Anyway, I got it to work. Thanks for your help. Now I have to make it
    stop on the last page without giving the error: Invalid
    CurrentPageIndex value. It must be >= 0 and < the PageCount.

    Travis Guest

  11. #10

    Default Re: Searched Datagrid with Paging?

    Travis, you should reset the CurrentPageIndex to 0 every time a new
    search is performed. The grid should not be displaying page numbers for
    pages that do not exist.

    Jason Bentley
    [url]http://geekswithblogs.net/jbenley[/url]

    Jason Bentley Guest

  12. #11

    Default Re: Searched Datagrid with Paging?

    Thanks Jason for all your help. Just wanted to get your thoughts on
    one thing. What books do you recommend for learning .NET?

    Travis

    Travis Guest

  13. #12

    Default Re: Searched Datagrid with Paging?

    [url]http://www.amazon.com/exec/obidos/tg/detail/-/0735614229/qid=1111698294/sr=8-5/ref=pd_csp_5/103-7047272-6136636?v=glance&s=books&n=507846[/url]
    [url]http://www.amazon.com/exec/obidos/tg/detail/-/1861007094/qid=1111698389/sr=8-5/ref=pd_csp_5/103-7047272-6136636?v=glance&s=books&n=507846[/url]
    [url]http://www.amazon.com/exec/obidos/tg/detail/-/0596003021/qid=1111698389/sr=8-6/ref=pd_csp_6/103-7047272-6136636?v=glance&s=books&n=507846[/url]

    Those three books should be on every one's bookshelves. Good luck.

    Jason Bentley
    [url]http://geekswithblogs.net/jbentley[/url]

    Jason Bentley Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139