使用标题中的文本框过滤gridview

时间:2014-09-23 09:19:34

标签: c# asp.net gridview datagridviewtextboxcell

所以我试图在标题中使用gridview过滤我的textboxes。我为每个textchanged创建了一个textbox方法。现在我想过滤gridview并绑定过滤后的数据。出于某种原因,datasource一旦达到textchanged方法就为空。请参阅下面的代码:

<asp:GridView ID="GridView_Imported" runat="server" CssClass="GridView-Upload" Width="100%" OnRowDataBound="GridView_Imported_RowDataBound" HorizontalAlign="Center" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="checkAll" runat="server" CssClass="checkAll" onclick="checkAll(this);" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkCtrl" runat="server" CssClass="chkCtrl" onclick="Check_Click(this)" />
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField AccessibleHeaderText="filter">
                    <HeaderTemplate>
                        <asp:Label ID="lbArticleName" runat="server" Text="Article-Name:" CssClass="Article-Name-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterArticleName" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticleName_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="ArticleNameText" runat="server" Text='<%# Eval("Article-Name") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                </asp:TemplateField>

                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbArticle" runat="server" Text="Article:" CssClass="Article-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterArticle" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticle_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbArticleText" runat="server" Text='<%# Eval("Article") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbWarehouse" runat="server" Text="Warehouse:" CssClass="Warehouse-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterWarehouse" runat="server" placeholder="Filter..." OnTextChanged="tbFilterWarehouse_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbWarehouseText" runat="server" Text='<%# Eval("Warehouse") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbLocation" runat="server" Text="Location:" CssClass="Location-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterLocation" runat="server" placeholder="Filter..." OnTextChanged="tbFilterLocation_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbLocationText" runat="server" Text='<%# Eval("Location") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>

            <HeaderStyle HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" />

        </asp:GridView>

        protected void tbFilterArticleName_TextChanged(object sender, EventArgs e)
        {
            var tb = (TextBox) sender as TextBox;

            GridView_Imported.DataSource = (Session["dataSource"] as DataTable).DefaultView.RowFilter = string.Format("Article-Name LIKE '%{0}%'", tb.Text.Trim());


            GridView_Imported.DataBind();
        }

我失踪了什么?

3 个答案:

答案 0 :(得分:0)

您的代码中有两个错误。

首先将列名称Article-Name的括号添加为[Article-Name]。

其次是将数据源分配给gridview的方式。请参阅下面的正确方法。

        var dt = (Session["dataSource"] as DataTable);
        dt.DefaultView.RowFilter = string.Format("[Article-Name] LIKE '%{0}%'", tb.Text.Trim());
        GridView_Imported.DataSource = dt;
        GridView_Imported.DataBind();

答案 1 :(得分:0)

使用http://www.datatables.net/

它提供数据过滤,排序,分页等。

只需在网格视图上方放置一个html表格标题,其中包含一些ID

<HeaderTemplate>
  <table id="tblData" border="0" cellpadding="5" cellspacing="1" width="100%">
    <thead>
       <th align="center" width="3%">S.No.&nbsp;
       </th>
       <th align="center" width="10%">RFQ ID &nbsp;
       </th>
    </thead>
</HeaderTemplate>

添加添加jquery脚本

$(document).ready(function () {
    $('#tblData').DataTable();
});

答案 2 :(得分:0)

首先,在gridview的RowDataBound事件中(找到你的文本框):

    System.Web.UI.WebControls.TextBox tbFilterArticle;
    protected void GridView_Imported_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView_Imported.HeaderRow != null)
        {
            tbFilterArticle = (System.Web.UI.WebControls.TextBox)GridView_Imported.HeaderRow.FindControl("tbSearchUser");
        }
    }

之后,要使用它的值(在任何你想要的地方,我将它用于按钮点击事件):

string article = tbFilterArticle.Text;