Asp.net projelerinde genelde verinin db den okunması için sqldatasource 
nesneleri kullanırız.Fakat kullanıcının arama yapması gibi durumlarda 
dinamik sql yaratmamız gerekebilir.Bu tür durumlarda Gridview nesnesinin 
datasource özelliğine codebehind da yarattığımız bir DataSet nesnesini 
atarız.
Örnek bir search
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conSQL = new SqlConnection(
            ConfigurationManager.
               ConnectionStrings["XConnectionString"].
                  ConnectionString);
        SqlCommand cmdSQL = new SqlCommand();
        
        string strSQL = "select x,y,z from a_tbl where ";
        if (this.txtX.Text.Trim().Length > 0)
        {
            strSQL += "x like @X and ";
            cmdSQL.Parameters.Add(new 
               SqlParameter("@X", this.txtX.Text.Trim()));
        }
        if (this.txtY.Text.Trim().Length > 0)
        {
            strSQL += "y like @Y and ";
            cmdSQL.Parameters.Add(
               new SqlParameter("@Y", this.txtY.Text.Trim()));
        }
        if (strSQL.Substring(strSQL.Length - 5, 5) == " and ")
            strSQL = strSQL.Substring(0, strSQL.Length - 5);
        if (strSQL.Substring(strSQL.Length - 7, 7) == " where ")
            strSQL = strSQL.Substring(0, strSQL.Length - 7);
        try
        {
            conSQL.Open();
        }
        catch (Exception excp)
        {
            this.lblSearchInfo.Text = "Hata oluştu -> " + 
               excp.Message;
            return;
        }
        cmdSQL.Connection = conSQL;
        cmdSQL.CommandText = strSQL;
        SqlDataReader drdSQL = null;
        DataSet dsSQL = new DataSet();
        try
        {
            drdSQL = cmdSQL.ExecuteReader();
        }
        catch (Exception excp)
        {
            conSQL.Close();
            this.lblSearchInfo.Text = excp.Message;
            return;
        }
        dsSQL.Load(drdSQL, LoadOption.OverwriteChanges,
            new string[] { "a_tbl" });
        drdSQL.Close();
        conSQL.Close();
        this.gvwCus.DataSource = dsSQL;
        this.gvwCus.DataBind();
    }
Son iki satırda gördüğünüz gibi gvwCus Gridview'umuza dsSQL dataset'ini
dinamik olarak bağlıyoruz.Fakat eğer gridview un paging özelliğini true 
olarak set edersek bu birsonraki sayfaya gitmeye kalktığımızda hata 
almamıza neden olacaktır.
Bu sorun can sıkıcı olsa da çözümü basittir.İlk olarak DataSet nesnesini 
ister class'ın en üstüne private static olarak tanımlayın, isterseniz 
ViewState veya Session bazında saklayın, dataset'ın sınıfın her 
üyesinden ulaşılabilecek hale getirin.
Bu durumda ben viewstate'i kullanıyorum.
Search'ün son iki satırını aşağıdaki gibi değiştiriyorum.
         ViewState["a_tbl"] = dsSQL;         
        this.gvwCus.DataSource = dsSQL;
        this.gvwCus.DataBind();
    }
Ardından Gridview'un PageIndexChanging eventine aşağıdaki kodu yazın.
    protected void gvwCus_PageIndexChanging(object sender,
      GridViewPageEventArgs e)
    {
        gvwCus.PageIndex = e.NewPageIndex;
        this.gvwCus.DataSource = (DataSet)ViewState["a_tbl"];
        gvwCus.DataBind();
    }
Sorununuz bu şekilde çözülmüş olur.