Created
February 24, 2014 21:20
-
-
Save kerenapura/9197381 to your computer and use it in GitHub Desktop.
Filter GridView by DropDownList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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