在DataTable上显示RowFilter以显示在gridview中 [英] RowFilter on a DataTable to display in a gridview

查看:144
本文介绍了在DataTable上显示RowFilter以显示在gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下似乎无效的代码.在Page_Load函数中,填充数据集并在网格视图中显示结果.

I have the following code which doesn't seem to work. In the Page_Load function I populate the DataSet and display the results in a grid view.

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作为参数传递):

I have some links which call this function to filter the data (yearID is passed as a parameter):

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.我还想念什么?

Yet the gridview shows the data it orignally loaded, and not the filtered data. The only thing I can think of is that I'm not using the DataTable in the Page_Load function. What else am I missing?

谢谢

艾德里安

推荐答案

将函数中的代码更改为:

Changed the code in the function to:

DataView dataView = newsDataSet.Tables[0].DefaultView;
dataView.RowFilter = "NewsDate2 Like '%" + yearID + "'";

GridViewMain.DataSource = dataView;
GridViewMain.DataBind();

在RowFilter语句中一定有东西.

It must have been something in the RowFilter statement.

这篇关于在DataTable上显示RowFilter以显示在gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆