GridView使用数据库实体搜索和过滤

时间:2017-01-27 05:22:47

标签: c# asp.net gridview

我是数据库模型的新手,我在输入文本框时尝试过滤 gridView。

  <asp:TextBox ID="TextBoxSearch" runat="server"></asp:TextBox>
<asp:GridView ID="myGridview" runat="server" AutoGenerateColumns="false"
        DataKeyNames="ContactID,MedicineTypeID,MedicineID" CellPadding="10" CellSpacing="0"
        ShowFooter="true"
        CssClass="myGrid"  HeaderStyle-CssClass="header" RowStyle-CssClass="trow1" 
        AlternatingRowStyle-CssClass="trow2" OnRowCommand="myGridview_RowCommand" OnRowCancelingEdit="myGridview_RowCancelingEdit" OnRowDeleting="myGridview_RowDeleting" OnRowEditing="myGridview_RowEditing" OnRowUpdating="myGridview_RowUpdating">

        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>Patient Name</HeaderTemplate>
                <ItemTemplate><%#Eval("PatientName") %></ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtPatientName" runat="server" Text='<%#Bind("PatientName") %>' />
                    <asp:RequiredFieldValidator ID="rfCPEdit" runat="server" ForeColor="Red" ErrorMessage="*"
                         Display="Dynamic" ValidationGroup="edit" ControlToValidate="txtPatientName">Required</asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtPatientName" runat="server"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="rfCP" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtPatientName">Required</asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>Quantity</HeaderTemplate>
                <ItemTemplate><%#Eval("Quantity") %></ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtQuantity" runat="server" Text='<%#Bind("Quantity") %>' />
                    <asp:RequiredFieldValidator ID="rfCNEdit" runat="server" ErrorMessage="*"
                        Display="Dynamic" ForeColor="Red" ValidationGroup="edit" ControlToValidate="txtQuantity">Required</asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="rfCN" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="txtQuantity">Required</asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>Medicine Type</HeaderTemplate>
                <ItemTemplate><%#Eval("MedicineType") %></ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddType" runat="server" AutoPostBack="true" 
                         OnSelectedIndexChanged="ddCountry_SelectedIndexChanged">
                        <asp:ListItem Text="Select Medicine type" Value="0"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfCEdit" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddType" InitialValue="0">
                        Required
                    </asp:RequiredFieldValidator>
                 </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddCountry_SelectedIndexChanged">
                        <asp:ListItem Text="Select Medicine Type" Value="0"></asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <asp:RequiredFieldValidator ID="rfC" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddType" InitialValue="0">Required</asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>Medicine Name</HeaderTemplate>
                <ItemTemplate><%#Eval("MedicineName") %></ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddState" runat="server">
                        <asp:ListItem Text="Select Medicine" Value="0"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfSEdit" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="edit" ControlToValidate="ddState" InitialValue="0">
                        Required
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddState" runat="server">
                        <asp:ListItem Text="Select Medicine" Value="0"></asp:ListItem>
                    </asp:DropDownList><br />
                    <asp:RequiredFieldValidator ID="rfS" runat="server" ErrorMessage="*"
                        ForeColor="Red" Display="Dynamic" ValidationGroup="Add" ControlToValidate="ddState"
                        InitialValue="0">Required</asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                    &nbsp;|&nbsp;
                    <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you confirm?')">Delete</asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" ValidationGroup="edit">Update</asp:LinkButton>
                    &nbsp;|&nbsp;
                    <asp:LinkButton ID="lbCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="btnInsert" runat="server" Text="Prescribe" CommandName="Insert" ValidationGroup="Add" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>

我想通过textBox过滤我的网格视图,其中 TextBox ID txtPatientName。有人可以帮我弄这个吗?

提前致谢!我只需要简要了解如何做到这一点。

1 个答案:

答案 0 :(得分:0)

添加文本框,即

<asp:TextBox ID="TextBoxSearch" runat="server" AutoPostBack="True"/>

并修改你的SqlDataSource,如下所述。

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [PatientName], [Quantity], [MedicineType],[ContactId],[MedicineTypeID],[MedicineID], [MedicineName] FROM [Patient]" FilterExpression="[PatientName] LIKE '%{0}%'" >
            <FilterParameters>
                <asp:ControlParameter ControlID="TextBoxSearch" DefaultValue="" Name="PatientName" PropertyName="Text" Type="String" />
            </FilterParameters>
        </asp:SqlDataSource>

经过测试和完美运作。希望能帮助到你。 此致!