borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (ADO.net sqlcommandbuilder Update/Insert/Delete Logic)Next Topic (MDB) New Topic New Poll Post Reply
AndreaVB Forum : Database : help! datagrid problem. Solved Topic
Poster Message
blacklisted
Level: Protégé

Registered: 08-09-2006
Posts: 8

icon help! datagrid problem.

im a beginner in vb, and ive got this problem regarding datagrid. how can i filter the datagrid using a combo box?
for example, ive got a combo box and got 3 list on it, elementary, high school, & college, and my datagrid in visible on the form. so, if i click elementary, how will my datagrid will only show students who are in elementary?
please help! my head is aching!
  please send an example code. thank you!

08-09-2006 at 01:42 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: help! datagrid problem.

Hello. You have to think in a "recordset shape".

Your DataGrid control is linked to a recordset, isn't it? (Otherwise, why are you using a DataGrid?). So it should expose a Filter property. If you have set the DataSource property on the recordset, the control should be refilled automatically when setting the Filter property.
In the case you didn't link the control to a recordset (so, you should use a lighter control, instead) when you have filled the control you retrieved the values by a recordset, so you can Filter it. Or even, you can re-execute the query, adding combobox' parameter to the string instruction.

Retrieving the parameter from the combo is the simplest task: it's enough to set each entry's ItemData property when filling combo's list, then read it when selecting an item and pass the value to the query.

These are the steps to go, if you need more info post the code you use to fill the grid and the combo.

Hope it helps.

____________________________
Real Programmer can count up to 1024 on his fingers

08-09-2006 at 10:03 AM
View Profile Send Email to User Show All Posts | Quote Reply
blacklisted
Level: Protégé

Registered: 08-09-2006
Posts: 8
icon Re: help! datagrid problem.

hi, you see i really am a beginner in this. i really dont now how to use filter can you complete this code for me?

i want to filter my datagrid like my example i have 3 list in combo box then if i click "Elementary" my datagrid must show all student in elementary level. how will i use adodc.recorset.filter property?

please correct this.

if combo1.text="Elementary" then
adodc.recordset.filter

what will i do next? i know its not complete. please show me an example! anyone! ill appreciate it! thanks!  

09-09-2006 at 05:17 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: help! datagrid problem.

quote:
blacklisted wrote:
please correct this.

if combo1.text="Elementary" then
adodc.recordset.filter


if combo1.text="Elementary" then
    adodc.recordset.filter = "TypeOfSchool = 'Elementary'"
Endif

Read the help for the Filter property: it has to be the text of a WHERE condition, without the keyword WHERE. Take care on the syntax (please notice that in my example I rounded the 'Elementary' word with single quotes, as required by SQLServer syntax), choosing it according to the data type of the field you have to filter and with the db you are connecting to.
All this, assuming you linked the datagrid to the adodc control. Otherwise, once more, post the code you connect it with.

____________________________
Real Programmer can count up to 1024 on his fingers
10-09-2006 at 01:19 PM
View Profile Send Email to User Show All Posts | Quote Reply
blacklisted
Level: Protégé

Registered: 08-09-2006
Posts: 8
icon Re: help! datagrid problem.

hey master yronium.
the filter works using combo box. thanks! but how can i use the textbox value for filtering? is it the same?
waiting for your reply. thanks!

15-09-2006 at 03:10 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: help! datagrid problem.

Well, you can try something like following:
    adodc.recordset.filter = "TypeOfSchool = " & Chr(39) & txtCriteria.Text & Chr(39)

In this sample I'm using single quotes again, the two Chr(39)s, for string delimiter but in fact you have to choose the appropriate syntax, according with the db engine you work with.

Only, you have to carefully choose what event you put the filtering instruction in: in fact, selecting an item in a combo raises the ComboBox_Click event, in which you can put the code. I mean: a combo selection is completed raising one single event. With a textbox you can't put your instructions in the Change event, because this event is fired on every typing into it, so if you are typing e. g. the "Elementary" word, the event - and the code into it - is run ten times, nine of them with an incomplete criteria.
When working with textboxes, I usually wait user completes his input, and then explicitly run my instructions by a command button. So I put the code in the Click event of an explicit commandbutton.

Hope it helps.

PS: hey, admin: it took a dozen attempts to me to succeed to send this current reply. Did you know about this misworking? (However, it seems going on properly now...)

[Edited by yronium on 15-09-2006 at 09:50 PM GMT]

____________________________
Real Programmer can count up to 1024 on his fingers

15-09-2006 at 08:39 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : help! datagrid problem. Solved Topic
Previous Topic (ADO.net sqlcommandbuilder Update/Insert/Delete Logic)Next Topic (MDB) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder