Link to home
Create AccountLog in
Avatar of Joe Brown
Joe Brown

asked on

Searchable gridview in Visual Studio / SQL / C#

I would like to make my gridview searchable. I have added the text box and the button for it. I need help with passing the parameter. Can someone help me to be able to make this gridview searchable by entering the field [Out_Lab_LabID]. I tried to add the parameter to the WHERE clause but was not successful.

    <asp:TextBox ID="SearchBox" runat="server"></asp:TextBox>
        <asp:Button ID="Search" runat="server" Text="Search" />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Out_Lab_LabID"
        DataSourceID="SqlDataSource1"
        EmptyDataText="There are no data records to display." Width="895px"
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>        
            <asp:hyperlinkfield  datatextfield="Out_Lab_LabID" HeaderText="Lab ID"
                    InsertVisible="False" SortExpression="Out_Lab_LabID"
                    datanavigateurlfields="Out_Lab_LabID"
                    datanavigateurlformatstring="LabEntry.aspx?LabID={0}"/>      
            <asp:TemplateField HeaderText="Lot" SortExpression="PC_Description">
                 <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Out_Lab_Lot") %>'></asp:Label>                
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Out_Lab_NonFTLot") %>'></asp:Label>    
            </ItemTemplate>
            </asp:TemplateField>        
                   
             <asp:BoundField DataField="Out_Lab_Type" HeaderText="Test Type"
                SortExpression="Out_Lab_Type" />
                   
             <asp:BoundField DataField="Out_Lab_PTCode" HeaderText="PT Code"
                SortExpression="Out_Lab_PTCode" />
               
             <asp:TemplateField HeaderText="Product Name" SortExpression="PC_Description">
                  <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Out_Lab_NonFTMatDescription") %>'></asp:Label>                
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("PC_Description") %>'></asp:Label>    
               <br />          
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Out_Lot_CustomProductName") %>'></asp:Label>    
                </ItemTemplate>
            </asp:TemplateField>    
             <asp:BoundField DataField="Out_Lab_SubDate" HeaderText="Submitted Date"
                SortExpression="Out_Lab_SubDate" DataFormatString="{0:dd MMM yyyy}" />    
            <asp:BoundField DataField="Out_Lab_ActualDate"
                HeaderText="Actual Date" SortExpression="Out_Lab_ActualDate"
                DataFormatString="{0:dd MMM yyyy}" />
            <asp:hyperlinkfield  datatextfield="Out_Lab_PDF" HeaderText="CofA"
                    InsertVisible="False" SortExpression="Out_Lab_Lot"
                    datanavigateurlfields="Out_Lab_Lot"
                    datanavigateurlformatstring="LabEntry.aspx?CofA={0}.pdf"/>      
        </Columns>
        <AlternatingRowStyle BackColor="#E3EAEB" />
    </asp:GridView>
   
         <asp:SqlDataSource ID="SqlDataSource1" runat= "server"
            ConnectionString="<%$ ConnectionStrings:FloratoryConnectionString %>"
            OldValuesParameterFormatString="original_{0}"        
            SelectCommand=" SELECT     Out_LabData.*, Out_LotAssigned.*, ProductCode.PC_Description
                            FROM       ProductCode
                            INNER JOIN Out_LotAssigned ON ProductCode.PC_ProductCode = Out_LotAssigned.Out_Lot_ProductCode
                            RIGHT OUTER JOIN Out_LabData ON Out_LotAssigned.Out_Lot_Lot = Out_LabData.Out_Lab_Lot
                            WHERE     (Out_LabData.IsDeleted = 0)
                            ORDER BY Out_LabData.Out_Lab_LabID DESC, Out_LabData.Out_Lab_SubDate DESC">                          
              <SelectParameters>
              <asp:ControlParameter ControlID="SearchBox" DefaultValue="%" Name="Out_LabID" PropertyName="Text"
                    Type="String" />
            </SelectParameters>
          </asp:SqlDataSource>
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi i have same task searchable gridview task done using this

http://www.aspdotnet-suresh.com/2011/12/search-records-in-gridview-and.html
Avatar of Joe Brown
Joe Brown

ASKER

I believe this would work as well. I chose to actually do a Response.Redirect and redirect to the same page when the LabID = the text in the search box (LabID)