2014-11-21 95 views
0

我是新來的c#asp.net。如何在Gridview中搜索數據?我的Gridview在頁面加載時具有來自數據庫的所有記錄。我一直在尋找它,大多數只是用一個空的GridView進行搜索。所以是的,我的不是空的。 錯誤:「GridView1」上定義了DataSource和DataSourceID。刪除一個定義。在Gridview中搜索數據c#asp.net

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

您應該粘貼你在這裏嘗試了一些代碼..... – yash 2014-11-21 06:45:15

+1

看到我的編輯職位 – missellorin 2014-11-21 06:49:31

+0

@Fel你有什麼問題?錯誤?網格視圖不可見? – yogi970 2014-11-21 06:53:57

回答

1

您無法從網格中獲取數據進行搜索,您需要將數據存儲在某處使用搜索的位置。即在每個搜索中存儲ViewState,Session或調用DataBase。 貝婁碼顯示存儲在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"]; 
    } 
} 
+0

嗨,SHAFEES。謝謝,但我得到同樣的錯誤。 :() – missellorin 2014-11-21 07:14:13

+0

刪除此DataSourceID =「.....」 - Kanniyappan – 2014-11-21 07:27:55

+0

我需要DataSourceID在頁面加載時從數據庫加載數據。 – missellorin 2014-11-21 07:34:42

1

@Fel ...我想你嘗試綁定使用的SqlDataSource從設計方以及數據數據網格作爲背後的代碼

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

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

代替

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

我需要它發生在onClick事件:( – missellorin 2014-11-21 07:28:42

+0

沒關係。是否綁定數據同時設計端以及代碼隱藏? 我告訴解決此問題「錯誤:DataSource和DataSourceID都在'GridView1'上定義。刪除一個定義。「 – Kanniyappan 2014-11-21 07:34:18

+0

已刪除。它的工作,但不是我想要的方式/我想先加載數據,然後搜索/ :( – missellorin 2014-11-21 07:58:02

0

請嘗試此代碼。

<%@ 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(); 
      } 
     } 
    } 
}