通过文本框c#asp.net搜索Gridview

时间:2015-04-09 16:42:45

标签: c# sql asp.net search gridview

我有一个使用存储过程接收信息的Gridview:

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;

这一切都运行正常,它从我的数据库中提取相关信息,我还设法将正在编辑的信息保存回数据库。

这花了我一些时间来做,因为我对这一切都是新手。

我现在正在尝试使用搜索文本字段来搜索gridview的内容,我尝试通过查询来做到这一点,但我感到有些困惑,因为我所看到的一切似乎想要重新绑定我的数据。

我只希望能够搜索正在显示的信息,因此我正在通过JQuery或Javascript进行此操作,这证明很难。

这是我的网格视图:

   <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>

如果有人能帮助我,我真的很感激!提前谢谢!

1 个答案:

答案 0 :(得分:0)

我原本想在gridview中搜索,我没有设法做到这一点,但我愿意接受我所做的事情。

Default.aspx.cs 我创建了我的连接字符串...我通过单击“服务器资源管理器”选项卡和属性来完成此操作,它在那里标记了你的连接字符串...

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");

然后我在我的文本字段旁边的按钮的click事件上写了以下内容。

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();

我希望这可以帮助那些和我在同一条船上的人。