Advertisement
some_yahoo

Find in Paged GridView

Sep 9th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace FindRowInPagedGrid
  9. {
  10.     public partial class _Default : System.Web.UI.Page
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {
  14.  
  15.         }
  16.  
  17.  
  18.         /// <summary>
  19.         /// Finds the item number, whatever page it's on.
  20.         /// </summary>
  21.         /// <param name="itemnumber">the global item number to find</param>
  22.         protected void SelectRow(int itemnumber)
  23.         {
  24.             int pagesize = GridView1.PageSize;
  25.             int targetpage = (int)Math.Floor((decimal)itemnumber / (decimal)pagesize);
  26.             int targetitem = itemnumber % pagesize;
  27.             GridView1.PageIndex = targetpage;
  28.             GridView1.SelectedIndex = targetitem;
  29.         }
  30.  
  31.         protected void SelectRow(string searchData)
  32.         {
  33.             int i = 0;
  34.             int theRow=0;
  35.  
  36.             //how to iterate over the whole grid
  37.             GridView1.AllowPaging = false;
  38.             GridView1.DataBind();
  39.  
  40.             for (i = 0; i < GridView1.Rows.Count; i++)
  41.             {
  42.                 //cell 2 is the data column, don't forget the command column.
  43.                 TableCell thisCell = GridView1.Rows[i].Cells[2];  
  44.  
  45.                 if (thisCell.Text == searchData)
  46.                     theRow = i;
  47.             }
  48.  
  49.             //rebind after finding the page
  50.             GridView1.AllowPaging = true;
  51.             GridView1.DataBind();
  52.  
  53.             SelectRow(theRow);
  54.         }
  55.  
  56.         protected void Button1_Click(object sender, EventArgs e)
  57.         {
  58.             SelectRow("California");
  59.         }
  60.  
  61.         protected void Button2_Click(object sender, EventArgs e)
  62.         {
  63.             SelectRow("Wyoming");
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement