فیلتر کردن GridView با استفاده از DropDownList در ASP.Net
با سلام در این مقاله با یک مثال توضیح خواهیم داد که چگونه می توان داده ها را در کنترل GridView با DropDownList در ASP.Net با استفاده از C # و VB.Net فیلتر کرد.
بانک اطلاعات
در اینجا از پایگاه داده Northwind استفاده می کنیم. که میتوانید از لینک زیر دانلود کنید .
پایگاه داده Northwind را بارگیری و نصب کنید
کد HTML
کد HTML شامل یک کنترل GridView است. DropDownList درون HeaderTemplate کنترل GridView قرار داده شده است.
DropDownList به یک رویداد handler در SelectedIndexChanged اختصاص داده شده است و ویژگی AutoPostBack روی True تنظیم شده است.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="10" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
HeaderStyle-BackColor="green" OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField>
<HeaderTemplate>
Country:
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="CountryChanged"
AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
<asp:ListItem Text="Top 10" Value="10"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Country") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PostalCode" HeaderText="Postal Code" />
</Columns>
</asp:GridView>
روش ذخیره شده
روش ذخیره شده زیر برای فیلتر کردن فایل های GridView استفاده می شود. مقدار انتخاب شده DropDownList به عنوان پارامتر Stored Procedure منتقل می شود.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE spx_GetCustomers
@Filter VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
IF @Filter = 'ALL'
SELECT ContactName, City, Country, PostalCode
FROM Customers
ELSE IF @Filter = '10'
SELECT TOP 10 ContactName, City, Country, PostalCode
FROM Customers
ELSE
SELECT ContactName, City, Country, PostalCode
FROM Customers WHERE Country=@Filter
END
GO
ساکن شدنGridView و DropDownList
GridView
در داخل رویداد Page Load ، متد BindGrid استفاده می شود. در داخل روش BindGrid ، Stored Procedure خوانده و مقدار پارامتر Filter ALL منتقل می شود ، تا بارگذاری شود.
جمعیت DropDownList
DropDownList با استفاده از روش BindCountryList ساکن شده است. در داخل روش BindCountryList ، DropDownList داخل HeaderTemplate از GridView با لیست متمایز از کشورها از جدول مشتریان ساکن شده است.
C #
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "ALL";
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("spx_GetCustomers");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList ddlCountry =
(DropDownList)GridView1.HeaderRow.FindControl("ddlCountry");
this.BindCountryList(ddlCountry);
}
private void BindCountryList(DropDownList ddlCountry)
{
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("select distinct Country" +
" from customers");
cmd.Connection = con;
con.Open();
ddlCountry.DataSource = cmd.ExecuteReader();
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataBind();
con.Close();
ddlCountry.Items.FindByValue(ViewState["Filter"].ToString())
.Selected = true;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("Filter") = "ALL"
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As New DataTable()
Dim strConnString As String = System.Configuration.ConfigurationManager _
.ConnectionStrings("conString").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim sda As New SqlDataAdapter()
Dim cmd As New SqlCommand("spx_GetCustomers")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Filter", ViewState("Filter"))
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim ddlCountry As DropDownList = DirectCast(GridView1.HeaderRow _
.FindControl("ddlCountry"), DropDownList)
Me.BindCountryList(ddlCountry)
End Sub
Private Sub BindCountryList(ByVal ddlCountry As DropDownList)
Dim strConnString As String = System.Configuration.ConfigurationManager _
.ConnectionStrings("conString").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim sda As New SqlDataAdapter()
Dim cmd As New SqlCommand("select distinct Country" & _
" from customers")
cmd.Connection = con
con.Open()
ddlCountry.DataSource = cmd.ExecuteReader()
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "Country"
ddlCountry.DataBind()
con.Close()
ddlCountry.Items.FindByValue(ViewState("Filter").ToString()) _
.Selected = True
End Sub
فیلتر کردن GridView با استفاده از DropDownList
در داخل دسته کنترل رویداد DropDownList's SelectedIndexChanged ، سوابق GridView بر اساس مقدار انتخاب شده DropDownList و تنظیم مقدار متغیر ViewState با مقدار انتخاب شده DropDownList فیلتر می شوند.
C #
protected void CountryChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
ViewState["Filter"] = ddlCountry.SelectedValue;
this.BindGrid();
}
VB.Net
Protected Sub CountryChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlCountry As DropDownList = DirectCast(sender, DropDownList)
ViewState("Filter") = ddlCountry.SelectedValue
Me.BindGrid()
End Sub
کار با صفحه بندی GridView
درون برنامه کنترل OnPageIndexChanging GridView ، ویژگی PageIndex از GridView به روز شده و روش BindGrid استفاده می شود.
C #
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
VB.Net
Protected Sub OnPaging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub