我使用此代码从DataGridView搜索和过滤数据,连接2表(内连接),但没有任何事情发生! 这是代码:
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
string sql = @"SELECT Invoice.[InvoiceID], Invoice.[CustomerID], Invoice.[Date], Invoice.[Amount], Invoice.[Paid], Invoice.[Rest], Invoice.[PaymentType], Invoice.[Shipped], Customer.[CustomerID], Customer.[Name], Customer.[Tell], Customer.[Address], Customer.[Comment] FROM [Invoice] INNER JOIN [Customer] ON Invoice.[CustomerID] = Customer.[CustomerID]";
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RechnungConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(sql, conn))
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
dt = new DataTable();
sda.Fill(dt);
cmd.ExecuteNonQuery();
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DataView DV = new DataView(dt);
DV.RowFilter = string.Format("Name LIKE '%{0}%'", TextBox1.Text);
GridView1.DataSource = DV;
}
这是Asp.net形式的Textbox和Gridview:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="InvoiceID" CssClass="table table-striped table-bordered" AllowSorting="true">
如果有人知道如何解决这个问题或者有更好的方法在GridView中搜索和过滤数据,请帮助我!
答案 0 :(得分:0)
Maziar,
您的文本框更改事件正在触发,但您的Page_Load会再次触发并加载您的网格。如果你把它放在调试中,你会看到你的页面加载再次激活并重置你的网格看起来像过滤器从未采取过。
如果您有实施第三方网格控制的预算,可以添加许多过滤功能,并且开箱即用。我有时没有使用标准网格,并且在我的所有网站中使用DevExpress网格。
请查看this link,了解DevExpress Grid带来的强大过滤效果。
该产品以您将保存的小时数为代价,以及您可以为最终用户提供的一系列功能。
答案 1 :(得分:0)
试试这个:
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt;
SqlCommand cmd2 = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
string sql = @"SELECT Invoice.[InvoiceID], Invoice.[CustomerID], Invoice.[Date], Invoice.[Amount], Invoice.[Paid], Invoice.[Rest], Invoice.[PaymentType], Invoice.[Shipped], Customer.[CustomerID], Customer.[Name], Customer.[Tell], Customer.[Address], Customer.[Comment] FROM [Invoice] INNER JOIN [Customer] ON Invoice.[CustomerID] = Customer.[CustomerID]";
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RechnungConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(sql, conn))
{
conn.Open();
sda.SelectCommand = cmd;
dt = new DataTable();
sda.Fill(dt);
cmd.ExecuteNonQuery();
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
dt = new DataTable();
cmd2 =
new SqlCeCommand(string.Format(@"SELECT Invoice.[InvoiceID], Invoice.[CustomerID], Invoice.[Date], Invoice.[Amount], Invoice.[Paid], Invoice.[Rest], Invoice.[PaymentType], Invoice.[Shipped], Customer.[CustomerID], Customer.[Name], Customer.[Tell], Customer.[Address], Customer.[Comment] FROM [Invoice] INNER JOIN [Customer] ON Invoice.[CustomerID] = Customer.[CustomerID] where Name LIKE '%{0}%'", TextBox1.Text));
try
{
adp1.SelectCommand = cmd2;
adp1.Fill(dt);
GridView1.DataSource = dt;
}
finally
{
con.Close();
}
}
如果您还有问题,请告诉我。 (Movafagh bashi)