DataGridView在C#中搜索和筛选数据
本文关键字:筛选 数据 搜索 DataGridView | 更新日期: 2023-09-27 18:20:02
我使用此代码从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">
如果有人知道如何解决这个问题,或者有更好的方法在GridViewplz中搜索和过滤数据,请帮助我!
试试这个:
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();
}
}
如果你还有问题,请告诉我。(莫瓦法赫·巴什)
Maziar,
您的文本框更改事件正在启动,但Page_Load将启动并再次加载网格。如果你把它放在调试中,你会看到你的页面加载再次启动,并重置你的网格,使其看起来像从未使用过的过滤器。
如果你有预算,实施第三方网格控制可以添加许多过滤功能,甚至更多开箱即用的功能。我已经有一段时间没有使用标准网格了,在我的所有网站上都使用DevExpress网格。
看看这个链接,你可以从DevExpress网格中获得强大的过滤功能。
该产品以你将节省的小时数和你可以为最终用户提供的一系列令人敬畏的功能为代价。