Skip to content

Instantly share code, notes, and snippets.

@kerenapura
Created February 24, 2014 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerenapura/9197381 to your computer and use it in GitHub Desktop.
Save kerenapura/9197381 to your computer and use it in GitHub Desktop.
Filter GridView by DropDownList
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint;
namespace SampleProject.DisplayGridView
{
public partial class DisplayGridViewUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Items("");
}
} //end Page_Load
protected void Filter_Items(Object sender, EventArgs e)
{
Load_Items(DropDownList1.SelectedValue);
} //end Filter_Items
protected void Load_Items(string filter)
{
//Select the current site
SPWeb thisWeb = SPContext.Current.Web;
SPQuery query = new SPQuery();
if (filter != "")
{
query.Query = string.Concat(
"<Where>",
"<Eq>",
"<FieldRef Name='Class' />",
"<Value Type='Text'>", filter, "</Value>",
"</Eq>",
"</Where>");
}
query.ViewFields = string.Concat(
"<FieldRef Name='ID' />",
"<FieldRef Name='Title' />",
"<FieldRef Name='Class' />");
query.ViewFieldsOnly = true;
//The list name is Sample List.
SPList thisList = thisWeb.Lists["Sample List"];
DataTable dt = new DataTable();
dt = thisList.GetItems(query).GetDataTable();
GridView1.DataSource = dt;
GridView1.AutoGenerateColumns = true;
GridView1.DataBind();
} //end Load_Items
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment