title: Filter records in gridview
body: <p>I wrote a small program to filter records in a grid view, filter by what is entered into a text box by the user. the problem with this are, the user cannot type fast,gridview takes time to display.  I want to make it faster so the user doesn't notice a delay. any suggestion to make this work better?</p>
  
  <pre><code>   &lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"    Inherits="_Default" %&gt;
  
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
       &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  
            &lt;script type="text/javascript"&gt;
               function runPostback() {
               document.forms["form1"].submit();
               document.getElementById("TextBox1").focus();
  
  
           }
   function getFocus(){
       var text = document.getElementById("TextBox1");
       if (text != null &amp;&amp; text.value.length &gt; 0) {
           if (text.createTextRange) {
               var FieldRange = text.createTextRange();
               FieldRange.moveStart('character', text.value.length); 
               FieldRange.collapse();
          FieldRange.select(); } }
          }
             function TriggerPostBack(control, arg){
  
  
        __doPostBack(control, arg);
           }
  
             function SetDelay() {
               // setTimeout("runPostback()",3000);
             }
  
  
  
  
  &lt;/script&gt;
  
   &lt;head runat="server"&gt;
   &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
   &lt;body onload="getFocus()"&gt;
  
  &lt;form id="form1" runat="server"&gt;
  &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
  &lt;/asp:ScriptManager&gt;
  
        &lt;div&gt; &lt;asp:TextBox ID="TextBox1" runat="server" Width="312px" OnTextChanged="Code_TextChanged" onkeyup="TriggerPostBack('control', 'arg')" &gt;&lt;/asp:TextBox&gt;
    &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
     &lt;Triggers&gt;
          &lt;asp:AsyncPostBackTrigger ControlID="TextBox1" /&gt;
      &lt;/Triggers&gt;
        &lt;ContentTemplate&gt;
  
            &lt;br /&gt;
            &lt;asp:GridView ID="GridView1" runat="server"&gt;
            &lt;/asp:GridView&gt;
  
        &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>------------------------------ SERVER SIDE CODE ---------------------------------------</p>
  
  <pre><code>   public partial class _Default : System.Web.UI.Page 
       {
              DataTable myDataTable = new DataTable();
  
  protected void Page_Load(object sender, EventArgs e)
  {
      //TextBox1.Attributes.Add("onkeyup", "Code_TextChanged");
  
   if (!IsPostBack)
      {
          GridView1.DataSource = generateDATATABLE();
          GridView1.DataBind();
      }
  
  
  }
  
  
  public void RaisePostBackEvent(string eventArgument) 
      {
          BindgrdView();
      }
  
  
  public DataTable generateDATATABLE()
  {
  
  
  
  
  
  
      DataColumn myDataColumn;
  
      myDataColumn = new DataColumn();
      myDataColumn.DataType = Type.GetType("System.String");
      myDataColumn.ColumnName = "ID";
      myDataTable.Columns.Add(myDataColumn);
  
      myDataColumn = new DataColumn();
      myDataColumn.DataType = Type.GetType("System.String");
      myDataColumn.ColumnName = "Link";
      myDataTable.Columns.Add(myDataColumn);
  
  
  
      myDataTable.Rows.Add("01", "G Powder");
      myDataTable.Rows.Add("02", "G Eye Drops");
      myDataTable.Rows.Add("03qq", "G Syrup");
      myDataTable.Rows.Add("0333", "G Syrup");
      myDataTable.Rows.Add("0453", "G Syrup");
      myDataTable.Rows.Add("0333", "G Syrup");
      myDataTable.Rows.Add("03666", "G Syrup");
      myDataTable.Rows.Add("0333", "G Syrup");
      myDataTable.Rows.Add("0453", "G Syrup");
      myDataTable.Rows.Add("033w8883", "G Syrup");
      myDataTable.Rows.Add("03666", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
      myDataTable.Rows.Add("048853", "t Syrup");
      myDataTable.Rows.Add("032233", "t Syrup");
      myDataTable.Rows.Add("0322666", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
      myDataTable.Rows.Add("048853", "t Syrup");
      myDataTable.Rows.Add("032233", "t Syrup");
      myDataTable.Rows.Add("0322666", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
      myDataTable.Rows.Add("048853", "t Syrup");
      myDataTable.Rows.Add("032233", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
      myDataTable.Rows.Add("048853", "t Syrup");
      myDataTable.Rows.Add("032233", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
      myDataTable.Rows.Add("048853", "t Syrup");
      myDataTable.Rows.Add("032233", "t Syrup");
      myDataTable.Rows.Add("038833", "t Syrup");
  
  
  
  
      return myDataTable;
  
  
  }
  
  protected void Code_TextChanged(object sender, EventArgs e)
  {
  
  
      myDataTable = generateDATATABLE();
  
  
      if (myDataTable.Rows.Count &gt; 0)
      {
          if (myDataTable.Rows.Count &gt; 0)
          {
  
  
              string filterExp = "ID &gt;= "+"'"+TextBox1.Text+"'";
              DataRow[] temp= myDataTable.Select(filterExp);
              GridView1.DataSource = convertTODT(temp);
              GridView1.DataBind();
          }
      }
  }
  
  
  public DataTable convertTODT(DataRow[] temp)
  {
      DataTable DT = new DataTable();
      DataColumn myDataColumn;
  
      myDataColumn = new DataColumn();
      myDataColumn.DataType = Type.GetType("System.String");
      myDataColumn.ColumnName = "ID";
      DT.Columns.Add(myDataColumn);
  
      myDataColumn = new DataColumn();
      myDataColumn.DataType = Type.GetType("System.String");
      myDataColumn.ColumnName = "Link";
      DT.Columns.Add(myDataColumn);
  
  
      foreach(DataRow dr in temp)
      {
          DT.ImportRow(dr);
      }
  
  
      return DT;
  }
     public void BindgrdView()
   {
  
      myDataTable = generateDATATABLE();
       if (myDataTable.Rows.Count &gt; 0)
      {
        GridView1.DataSource = myDataTable.Select("ID like" + "'" +      TextBox1.Text.ToString() + "'");
         GridView1.DataBind();
       }
    }
  
  }
  </code></pre>