简体   繁体   中英

Search Gridview via textbox c# asp.net

I have a Gridview which is using a stored procedure to receive information:

CREATE PROCEDURE sproc_getTableContentStock
@StockID int = 0,
@ClientID int = 0,
@ItemTypeID int = 0,
@ItemName varchar(30) = null,
@DateAddedToSystem varchar(50) = null,
@DateSold varchar(50) = null,
@DatePurchased varchar(50) = null,
@ItemDescription varchar(50) = null,
@RetailPrice money = 0 ,
@Quantity_ smallInt = 0,
@ItemSold varchar(3) = null,
@Designer varchar(50) = null,
@PurchasePrice money = null,
@ItemColour varchar(20) = null,
@ItemSize varchar(5) = null
AS 

SET NOCOUNT ON;

select  StockID ,
ItemName ,
DateSold ,
DatePurchased ,
ItemDescription ,
RetailPrice 
Quantity_ ,
ItemSold ,
Designer ,
PurchasePrice,
ItemColour,
ItemSize,
ItemType, 
RetailPrice,
FirstName

From Stock

inner join ItemTypes on Stock.ItemTypeID = ItemTypes.ItemTypeID

inner join Clients on Stock.ClientID = Clients.ClientID;

This is all working fine, and it is pulling the relevant information from my database, I have also managed to get the information which is being edited save back to the database.

This took me some time to do as I'm new to all of this.

I am now trying to have a search textfield to search the contents of my gridview, I have tried doing this via a query, but I'm getting a little confused as everything I've looked at seems to want to rebind my data.

I only want to be able to search for information which is being displayed therefore I was looking at doing this via JQuery or Javascript, this is proving difficult.

Here is my grid view:

   <asp:GridView ID="gridStock" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="StockData" CssClass="stocktables table table-responsive table-hover"  AutoGenerateEditButton="True" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover" DataKeyNames="StockID" BackColor="White">
            <Columns>
                <asp:BoundField DataField="StockID" HeaderText="StockID" SortExpression="StockID" HeaderStyle-CssClass="hidden" ItemStyle-CssClass="hidden" >
                </asp:BoundField>
                <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="ItemDescription" HeaderText="ItemDescription" SortExpression="ItemDescription" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                 <asp:BoundField DataField="Designer" HeaderText="Designer" SortExpression="Designer" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="Quantity_" HeaderText="Quantity" SortExpression="Quantity_" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="ItemSize" HeaderText="ItemSize" SortExpression="ItemSize"  HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover"/>
                <asp:BoundField DataField="ItemType" HeaderText="ItemType" SortExpression="ItemType"  HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover" />
                <asp:BoundField DataField="RetailPrice" HeaderText="RetailPrice" SortExpression="RetailPrice"  HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover"/>
                <asp:BoundField DataField="ItemColour" HeaderText="ItemColour" SortExpression="ItemColour" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover"></asp:BoundField>
                <asp:BoundField DataField="DateSold" HeaderText="DateSold" SortExpression="DateSold" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="DatePurchased" HeaderText="Date Purchased" SortExpression="DatePurchased" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="ItemSold" HeaderText="Item Sold" SortExpression="ItemSold" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="PurchasePrice" HeaderText="Purchased Price" SortExpression="PurchasePrice" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover">
                </asp:BoundField>
                <asp:BoundField DataField="FirstName" HeaderText="Client Name" SortExpression="FirstName" ReadOnly="True" HeaderStyle-CssClass="table table-responsive" ItemStyle-CssClass="table table-responsive table-hover"/>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="StockData" runat="server" ConnectionString="<%$ ConnectionStrings:LabelsDressShopConnection %>" SelectCommand="sproc_getTableContentStock"
            SelectCommandType="StoredProcedure" UpdateCommand="UPDATE [Stock] set [ItemName]=@ItemName, [ItemDescription]=@ItemDescription, [ItemSize]=@ItemSize, [Designer]=@Designer, [DatePurchased]=@DatePurchased, [DateSold]=@DateSold, [RetailPrice]=@RetailPrice, [ItemColour]=@ItemColour Where [StockID]=@StockID"></asp:SqlDataSource>

If any one could help me I'd be truly grateful! Thanks in advance!

I originally wanted to search within the gridview, I haven't managed to do that but I'm willing to settle for what I have done.

Default.aspx.cs I created my connection string... I done this by clicking on the "Server explorer" tab and properties, it is labled there what your connection string is...

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Sophia\Desktop\fyp-dressshop\FYP-LabelsDressShopLeicesterSystem\LabelsShopApplication\App_Data\LabelsDressShop.mdf;Integrated Security=True;Connect Timeout=30");

I then wrote the following on the click event of my button next to my text field.

String str = "select * from Stock where (ItemName like '%' + @search + '%')";// saving my query as a string variable
SqlCommand xp = new SqlCommand(str, conn);
xp.Parameters.Add("@search", SqlDbType.VarChar).Value = txtSearch.Text;
//opening my connection 
con.Open();

//Excuting my command which i saved as a string 
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;

DataSet ds = new DataSet();
//Has to be the same name a in the DB
da.Fill(ds, "ItemName");

GridView1.DataSource = ds;
GridView1.DataBind();


conn.Close();

I hope this helps anyone who was in the same boat as me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM