shraddha
shraddha

Reputation: 11

create a filter row with textboxes on each column in asp.net gridview

I have a asp.net gridview, which i am binding at runtime with a custom List object. I want to add a filter row below the header row on each column and on click of filter button grid data should get filtered based on values written in the filter textboxes. requirement seems weird but this is what client wants. please help with some clue.

Upvotes: 0

Views: 3039

Answers (2)

R.Akhlaghi
R.Akhlaghi

Reputation: 760

aspx code :

 <asp:TemplateField>
                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="150px" />
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <td align="center">
                                    <asp:ImageButton runat="server" ID="imgFilter1" ImageUrl="../Images/filter.png" Style="height: 20px;
                                        width: 20px;" OnClick="imgFilter1_click" />
                                </td>
                                <td align="center">
                                    <asp:TextBox runat="server" ID="gridTextboxFilter1" AutoPostBack="true" onTextChanged="gridTextboxFilter1_text_changed">
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" colspan="2">
                                    //your column header
                                </td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Eval("your_dataFeild") %>'>
                        </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

cs code :

private void BindGrid(string strFilter)
        {            
            try
            {
                // Simple created a table to bind with Grid view and 
                // populated it with data.
                DataTable dt = new DataTable("sample");
                dt.Columns.Add("ID");
                dt.Columns.Add("Name");
                DataRow dr ;
                for(int counter=1;counter<11;counter++)
                {
                    dr = dt.NewRow();
                    dr["ID"]=counter.ToString();
                    dr["Name"]= "Cat" + counter.ToString();
                    dt.Rows.Add(dr);
                }

                DataView dv = new DataView(dt);
                if(strFilter != "")
                    dv.RowFilter="Name like '%" + strFilter + "%'";

                if (CategoryFilter == "")
                    gvCategory.DataSource = dv;
                else
                    gvCategory.DataSource = dv;
                gvCategory.DataBind();
            }
            catch (Exception ex)
            {

            }
            finally
            {

            }
        }

        protected void gridTextboxFilter1_text_changed(object sender, EventArgs e)
        {
            string text = ((TextBox)sender).Text;
            BindGrid(text);
        }

Upvotes: 1

kbvishnu
kbvishnu

Reputation: 15630

Add a textbox and button on the header template.

Write a query on button press and get the value.

The query something like select * from tbl where col like '%val%'

Bind the value to gridView.

I think this will solves for you

Upvotes: 0

Related Questions