Solved

RowFilter

Posted on 2009-05-08
5
571 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-
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 500 total points
ID: 24342545
dv = custdt.DefaultView;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now