Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RowFilter

Posted on 2009-05-08
5
Medium Priority
?
580 Views
Last Modified: 2012-05-06
I'm using RowFilter to sort a gridview when a dropdown menu selectedIndex is changed

when the page loads, the gridview displays and the dropdown menu is populated.

but, when I make a selection in the dropdown menu, the gridview comes up blank.

I've tried just hardcoding a value for the RowFilter to see if that would work and the gridview still comes up blank.

Any help is appreciated.
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataViewRowFilterExample.NorthwindTableAdapters;
 
namespace DataViewRowFilterExample
{
    public partial class _Default : System.Web.UI.Page
    {
        CustomersTableAdapter custAdapter = new CustomersTableAdapter();
        CountriesTableAdapter countryAdapter = new CountriesTableAdapter();
        Northwind.CustomersDataTable custdt = new Northwind.CustomersDataTable();
        Northwind.CountriesDataTable countrydt = new Northwind.CountriesDataTable();
        Northwind ds = new Northwind();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            custAdapter.Fill(custdt);
            countryAdapter.Fill(countrydt);
 
            if (!Page.IsPostBack)
            {
                ddlCountries.DataSource = countrydt;
                ddlCountries.DataValueField = "Country";
                ddlCountries.DataTextField = "Country";
                ddlCountries.DataBind();
           
                gvCustomers.DataSource = custdt;
                gvCustomers.DataBind();
            }
        }
 
        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex = ddlCountries.SelectedIndex;
 
            DataView dv = new DataView();
            //dv.RowFilter = "Country = " + ddlCountries.SelectedItem.Value.ToString();
            dv.RowFilter = "Country = 'USA'";
            gvCustomers.DataSource = dv;
            gvCustomers.DataBind();
 
            ddlCountries.SelectedIndex = selectIndex;
        }
    }
}

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24340979
I would suggest a different course of action--use the DataView and the RowFilter all the time, and not just in the DropDownList_SelectedIndexChanged event handler.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24341864
U missed out this sentence...

DataView dv = custdt.Tables[0].DefaultView;

The code should look like this

DataView dv = custdt.Tables[0].DefaultView;
dv.RowFilter = "Country = 'USA'";
            gvCustomers.DataSource = dv;
            gvCustomers.DataBind();
0
 
LVL 7

Expert Comment

by:ajolly
ID: 24341865
of course..

You are just saying

DataView dv = new DataView();

which will create only empty view for you.
and you are binding with this empty view... so definitely .. you should see no records.

Probably your view should come from the datatable custdt
0
 

Author Comment

by:-Dman100-
ID: 24342422
I tried the following, but the datagrid is still empty?  Is this not the correc syntax?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataViewRowFilterExample.NorthwindTableAdapters;
 
namespace DataViewRowFilterExample
{
    public partial class _Default : System.Web.UI.Page
    {
        CustomersTableAdapter custAdapter = new CustomersTableAdapter();
        CountriesTableAdapter countryAdapter = new CountriesTableAdapter();
        Northwind.CustomersDataTable custdt = new Northwind.CustomersDataTable();
        Northwind.CountriesDataTable countrydt = new Northwind.CountriesDataTable();
        Northwind ds = new Northwind();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            custAdapter.Fill(custdt);
            countryAdapter.Fill(countrydt);
 
            if (!Page.IsPostBack)
            {
                ddlCountries.DataSource = countrydt;
                ddlCountries.DataValueField = "Country";
                ddlCountries.DataTextField = "Country";
                ddlCountries.DataBind();
           
                gvCustomers.DataSource = custdt;
                gvCustomers.DataBind();
            }
        }
 
        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex = ddlCountries.SelectedIndex;
 
            DataView dv = new DataView();
            dv = ds.Customers.DefaultView;
            //dv.RowFilter = "Country = " + ddlCountries.SelectedItem.Value.ToString();
            dv.RowFilter = "Country = 'USA'";
            gvCustomers.DataSource = dv;
            gvCustomers.DataBind();
 
            ddlCountries.SelectedIndex = selectIndex;
        }
    }
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 24342545
dv = custdt.DefaultView;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question