ドロップボックス リストと検索語に基づいて GridView データをフィルター処理しようとしています。
私の GridView は現在、SQLdatasource を使用して ASP サーバー側でバインドされています。
onclick_button イベントから C# を介して GridView をフィルター処理しようとしています。
これが私のフィルタリングコードです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace ViewCDs
{
public partial class _Default : System.Web.UI.Page
{
protected void btnSearch_Click(object sender, EventArgs e)
{
string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Dave\Documents\cdsCollections.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlcon = new SqlConnection(connstring);
string str = TextBox1.Text;
string str2 = DropDownList1.Text;
string set = "";
if (str2 == "Artist")
{
set = "artist";
}
if (str2 == "CD Title")
{
set = "cdTitle";
}
if (str2 == "Music Genre")
{
set = "genre";
}
SqlCommand sqlcmd = new SqlCommand("select * from cds where " + set + " like '%" + str + "%'", sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}
}
また、ASP 側でイベントがトリガーされる方法は次のとおりです。
<asp:Button ID="Button1" runat="server" Text="Search" onclick="btnSearch_Click" />
これは非常に標準的なレイアウトです。ワイルドカード クエリの結果はデータセット オブジェクトに格納され、データセットのカウントが 0 より大きい場合は、GridView にバインドする必要があります。しかし、このバインディングは行われておらず、次のエラーが発生しています。
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.