2014-11-21 61 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(); 
      } 
     } 
    } 
}