我的 asp.net 表单中有四个字段,即购买日期、类别、项目名称和项目代码。如果我们在其中任何一个字段中输入值,其他三个字段信息必须显示在网格视图中。假设,如果我们只输入购买日期,该日期对应的详细信息将显示在 gridview 中。我想要存储过程以及cs代码。
问问题
106 次
1 回答
0
如果我理解正确并且我们假设您使用 SQL,那么它应该如下所示:
protected void YourEventThatStart(object sender, EventArgs e)
{
YourGrid.DataSource = GetDataTable("SELECT PurchaseDate, Category, ItemName, ItemCode FROM sometable WHERE PurchaseDate like '%" + Field1.Text + "%' and Category like '%" + Field2.Text + "%' and ItemName like '%" + Field3.Text + "%' and ItemCode like '%" + Field3.Text + "%'");
}
public DataTable GetDataTable(string query)
{
SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(query, conn);
DataTable myDataTable = new DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
conn.Dispose();
}
return myDataTable;
}
如果您不多次使用 selectcommand,则可以在事件中删除public DataTable GetDataTable(string query)
并使用该代码。YourEventThatStart
现在,您只需YourEventThatStart
在需要填充网格时执行该事件,无论您想要什么事件。
于 2013-06-18T13:34:05.727 回答