我有以下代码似乎不起作用。在Page_Load函数中,我填充DataSet并在网格视图中显示结果。
newsCommand = new SqlCommand("SQL code here", dbConnection);
newsDataSet = new DataSet();
newsDataAdapter = new SqlDataAdapter(newsCommand);
newsDataAdapter.SelectCommand = newsCommand;
newsDataAdapter.Fill(newsDataSet, "Bulletins");
if (!Page.IsPostBack)
{
GridViewMain.DataSource = newsDataSet;
GridViewMain.DataBind();
}
我有一些链接调用此函数来过滤数据(yearID作为参数传递):
DataTable newsTable = new DataTable();
newsTable = newsDataSet.Tables[0];
DataView dvData = new DataView(newsTable);
dvData.RowFilter = "Year > '" + yearID + "'";
GridViewMain.DataSource = dvData;
GridViewMain.DataBind();
然而,gridview显示它正在加载的数据,而不是过滤后的数据。我唯一能想到的是我没有在Page_Load函数中使用DataTable。我还缺少什么?
谢谢,
阿德里安
答案 0 :(得分:7)
将函数中的代码更改为:
DataView dataView = newsDataSet.Tables[0].DefaultView;
dataView.RowFilter = "NewsDate2 Like '%" + yearID + "'";
GridViewMain.DataSource = dataView;
GridViewMain.DataBind();
一定是RowFilter语句中的内容。
答案 1 :(得分:0)
从 DataTable 中过滤数据并显示在 Gridview 中。
string category = ddlcat.SelectedItem.Value; // this can be any input by user
DataTable dt = filter_dt; //filter_dt is DataTable object, contains actual data, from there we will filter
DataView dataView = dt.DefaultView;
if (!string.IsNullOrEmpty(category))
{
dataView.RowFilter = "Category = '" + category + "'";
}
Gridview1.DataSource = dataView;
Gridview1.DataBind();
如有疑问,请随时 询问 。
谢谢