Pages

Friday, 17 February 2012

Multiple Column Filter Using Asp.Net with C#

Here I will explain Multiple Column Filter Using Asp.Net with C#.

//passing querystring with fields
if (items == string.Empty)
            {
                if (GvList.Rows.Count > 0)
                {
                    clcnt = GvList.Columns.Count;
                    shipal.Clear();
                    for (int i = 1; i < clcnt - 2; i++)
                    {
                        if (i != 7)
                        {
                            shipal.Add(GvList.HeaderRow.Cells[i].Text);
                        }
                    }
                    items = string.Empty;
                    items = String.Join(",", ((string[])shipal.ToArray(typeof(String))));
                }
            }
            ImgFilter.Attributes.Add("onclick", "window.showModalDialog('Filter.aspx?" + EncryptQueryString(string.Format("QString={0}", items"','','dialogWidth:400px;dialogHeight:400px;center:yes;edge:raised;resizable:no;scroll:off;status:yes')");


//.aspx code
  <table>
        <tr>
            <td colspan="2">
                <asp:Panel ID="pnl" runat="server" BorderColor="LightGray" Width="385px" Height="300px"
                    ScrollBars="Auto">
                    <asp:GridView ID="gvfilter" runat="server" AutoGenerateColumns="False" Width="367px"
                        Font-Names="Tahoma" Font-Size="8pt" CellPadding="4" ForeColor="#333333" BackImageUrl="~/Assets/img/tab9.png"
                        HeaderStyle-Height="30px">
                        <HeaderStyle CssClass="GridViewHeaderStyle" />
                        <Columns>
                            <asp:TemplateField HeaderText="Field">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlfilter" runat="server" Width="125px" Font-Names="Tahoma"
                                        Font-Size="8pt">
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Filter">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtfilter" runat="server" Width="200px" BackColor="#F7F6F3" BorderStyle="None"
                                        Font-Names="Arail" Font-Size="8pt"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </td>
        </tr>
        <tr>
            <td align="right" class="style1">
            </td>
            <td class="style1">
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Button ID="BtnOk" runat="server" OnClick="BtnOk_Click" Text="OK" Font-Names="Arial"
                    Width="75px" Font-Size="8pt" />
                &nbsp;
                <asp:Button ID="BtnCancel" runat="server" Font-Names="Arial" Font-Size="8pt" Width="75px"
                    Text="Cancel" OnClientClick="window.close();" />
            </td>
        </tr>
    </table>


//.cs code
if (Request.QueryString["QString"] != null)
                {
                    if (!string.IsNullOrEmpty(QString))
                    {
                        string[] arry = Request.QueryString["QString"].ToString().Split(',');
                        gvfilter.DataSource = arry;
                        gvfilter.DataBind();
                        foreach (string item in arry)
                        {
                            foreach (GridViewRow row in gvfilter.Rows)
                            {
                                ddl = (DropDownList)row.FindControl("ddlfilter");
                                ddl.Items.Add(item);
                            }
                        }
                        foreach (GridViewRow row in gvfilter.Rows)
                        {
                            ddl = (DropDownList)row.FindControl("ddlfilter");
                            ddl.Items.Insert(0, "");
                        }
                    }
                }


//ok button

protected void BtnOk_Click(object sender, EventArgs e)
[

for (int i = 0; i < gvfilter.Rows.Count; i++)
{

ddl = (
DropDownList)gvfilter.Rows[i].FindControl("ddlfilter");

txt = (TextBox)gvfilter.Rows[i].FindControl("txtfilter");

al.Add(ddl.SelectedValue);

alfltr.Add(txt.Text);

}

string items = String.Join(",", ((string[])al.ToArray(typeof(String))));

string fltrs = String.Join(",", ((string[])alfltr.ToArray(typeof(String))));

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "A", "<script>window.returnValue = true;window.close();</script>");

if (SList != null)

{

Session[
"fil1"] = al;

Session["txtfil1"] = alfltr;

}

else

{

Session[
"fil"] = al;

Session["txtfil"] = alfltr;

}

 

}

No comments:

Post a Comment