簡體   English   中英

在Gridview中搜索數據c#asp.net

[英]searching data in Gridview c# asp.net

我是C#asp.net的新手。 如何在Gridview中搜索數據? 我在頁面加載時的Gridview具有數據庫中的所有記錄。 我一直在搜索它,大多數只是使用一個空的GridView搜索。 是的,我的不是空的。 錯誤:DataSource和DataSourceID均在“ GridView1”上定義。 刪除一個定義。

        String str = "select * from tblEmployee where (Name like '%' + @search + '%')";
        SqlCommand xp = new SqlCommand(str, objsqlconn);
        xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
        objsqlconn.Open();
        xp.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = xp;
        DataSet ds = new DataSet();
        da.Fill(ds, "Name");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        objsqlconn.Close();

您無法從網格中獲取數據進行搜索,您需要將數據存儲在使用搜索的位置。 即在每次搜索時將其存儲在ViewState,Session或調用數據庫中。 波紋管代碼顯示了存儲在ViewState中的數據,您可以隨時使用GridViewData進行訪問,從而可以隨時訪問數據。 (如果您有大量數據,則首選在每次搜索時從數據庫中調用數據。)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = GridViewData;
        GridView1.DataBind();
    }
}


public DataSet GridViewData
{
    get
    {
        if (ViewState["GridViewData"] == null)
        {
            String str = "select * from tblEmployee where (Name like '%' + @search + '%')";
            SqlCommand xp = new SqlCommand(str, objsqlconn);
            xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
            objsqlconn.Open();
            xp.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = xp;
            DataSet ds = new DataSet();
            da.Fill(ds, "Name");
            objsqlconn.Close();

            ViewState["GridViewData"] = ds;
        }

        return (DataSet)ViewState["GridViewData"];
    }
}

@Fel ...我認為您嘗試使用sqldatasource從設計方面以及代碼的背面將數據綁定到數據網格

1)您需要選擇一種方法來綁定網格2)如果您在設計方面進行綁定,請從網格視圖設計中刪除DataSourceID屬性。 這樣使用

asp:gridview id="grdData" runat="server"

代替

asp:gridview id="grdData" runat="server" DataSourceID="Datasource1"

請嘗試此代碼。

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Search GridView Record on Button Click By Using C#.Net in Asp.Net</title> </head> <body> <form id="form1" runat="server"> Search By Title : <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display." Width="500px" BorderStyle="Solid" ShowFooter="True"> <Columns> <asp:BoundField DataField="author_name" HeaderText="NAME" /> <asp:BoundField DataField="publisher_name" HeaderText="PUB. NAME" /> <asp:BoundField DataField="title" HeaderText="TITLE" /> <asp:BoundField DataField="publication_year" HeaderText="PUB. YEAR" /> </Columns> <HeaderStyle BackColor="#66CCFF" /> </asp:GridView> </form> </body> </html> 

現在,只需檢查.cs頁面上的代碼即可。

using System;
using System.Data;
using System.Data.OleDb;
namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase("");
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
            }
        }

        /// 
        /// Function for binding retribing the data from database
        /// In this i have used Access DB you can use SQL DB to bind the data
         ///
        public DataTable GetDataFromDataBase(string searchtext)
        {
            DataTable _objdt = new DataTable();
            string querystring = "";
            querystring = "select * from Books";
            if (querystring != "")
            {
                querystring += " where title like '%" + txtsearch.Text + "%';";
            }
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase(txtsearch.Text);
            if (_objdt.Rows.Count > 0)
            {
                GridView1.DataSource = _objdt;
                GridView1.DataBind();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM