Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2008, 07:54 AM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Nellemandela
Default Filtering combobox from a dataview.

Hi im trying to "bind" some comboboxes together. But im stuck.

I have 3 comboboxes im trying to bind.

cbxType (holds all values (Name,Id) from the Type Table)
cbxMake (holds all values (Name,Id) where TypeId = selectedindex from cbxType from the Make Table)
cbxModel (holds all values where (Name,Id) MakeId = selectedindex from cbxMake from the Model Table)

I suppose im stuck at (and below)

DataView makeView = new DataView(ds.Tables["Make"])

Code:
My database:

Table: Type
Id (Int)
Name(String)

Table: Make
Id (Int)
TypeId (Int)
Name(String)

Table: Model
Id (Int)
MakeId (Int)
Name(String)
Code:
        
// Create dataset

ds = new DataSet("TypeMakeModel");

//Tablemapping "Type"

command.CommandText = "SELECT * FROM Type";

OleDbDataAdapter da1 = new OleDbDataAdapter(command);

da1.TableMappings.Add("Table", "Type");

da1.Fill(ds);

//Tablemapping "Make"

command.CommandText = "SELECT * FROM Make";

OleDbDataAdapter da2 = new OleDbDataAdapter(command);

da2.TableMappings.Add("Table", "Make");

da2.Fill(ds);



dsView = ds.DefaultViewManager;

cbxType.DataSource = dsView;
cbxType.DisplayMember = "Type.Name";
cbxType.ValueMember = "Type.Id";



DataView makeView = new DataView(ds.Tables["Make"]);
int typeid = cbxType.SelectedIndex;
makeView.RowFilter = "TypeId = " + typeid;



cbxMake.DataSource = makeView;
cbxMake.DisplayMember = "Make.Name";
cbxMake.ValueMember = "Make.Id";



cbxModel.DataSource = dsView;
cbxModel.DisplayMember = "Model.Name";
cbxModel.ValueMember = "Model.Id";
 
Old August 27th, 2008, 12:55 AM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you want a ripple effect of changing one should affect other then you need to handle that in an event. For example in this case you need to set the DataSource properties in the SelectedChange event of the cbxType for cbxMake

so in cbxMake SelectedChange event set the DataSource i.e. you have the following code

DataView makeView = new DataView(ds.Tables["Make"]);
int typeid = cbxType.SelectedIndex;
makeView.RowFilter = "TypeId = " + typeid;



cbxMake.DataSource = makeView;
cbxMake.DisplayMember = "Make.Name";
cbxMake.ValueMember = "Make.Id";

cbxModel.DataSource = dsView;
cbxModel.DisplayMember = "Model.Name";
cbxModel.ValueMember = "Model.Id";



It is not how much we do,
but how much love we put in the doing.

-Mother Theresa
 
Old August 27th, 2008, 03:49 AM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Nellemandela
Default

Thanks the code you posted somewhat solved my problem. But a new problem has emerged.


The code below throws this error. {"Unable to cast object of type 'System.Data.DataViewManagerListItemTypeDescriptor ' to type 'System.IConvertible'."}

i need to use the SelectedValue because there might be gaps in id's if a type has been deleted.


Code:
    private void cbxType_SelectedValueChanged(object sender, EventArgs e)
    {
        label4.Text = cbxType.SelectedValue.ToString();
        makeView = new DataView(ds.Tables["Make"]);
        int filter = Convert.ToInt32(cbxType.SelectedValue);

        makeView.RowFilter = "TypeId = " + filter;

        cbxMake.DataSource = makeView;
        cbxMake.DisplayMember = "Name";
        cbxMake.ValueMember = "Id";
    }
 
Old August 28th, 2008, 02:10 PM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Nellemandela
Default

Found this link that solved all my problems.

http://www.vbforums.com/showthread.php?t=518067

Thank you for you help





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling Combobox with value from other Combobox ayazhoda Access VBA 6 June 5th, 2007 04:58 AM
dataview Yasho VB.NET 2002/2003 Basics 1 May 28th, 2007 03:40 AM
Filtering Columns in dataview ramesh055 ASP.NET 1.0 and 1.1 Professional 1 November 3rd, 2006 03:51 PM
Filtering Dataview [Strange Problem] anup_daware ADO.NET 1 August 8th, 2006 07:09 AM
data filtering from combobox MAKO C# 4 June 1st, 2006 03:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.