Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RowFilter on a DataTable to display in a gridview

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();
}

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();

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?

Thanks,

Adrian

like image 494
Adrian Johnson Avatar asked Aug 10 '10 14:08

Adrian Johnson


People also ask

Can you use a DataView to filter rows in a data table?

Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.

What is RowFilter DataView?

Gets or sets the expression used to filter which rows are viewed in the DataView. public: virtual property System::String ^ RowFilter { System::String ^ get(); void set(System::String ^ value); };


1 Answers

Changed the code in the function to:

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

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

It must have been something in the RowFilter statement.

like image 176
Adrian Johnson Avatar answered Nov 15 '22 11:11

Adrian Johnson