Click here to Skip to main content
15,888,096 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
INTRODUCTION AND RELEVANT INFORMATION:

Before I start, it is important to introduce example SQL table that will be used in this question to illustrate the problems I face:
SQL
Table < #ID autonumber, Planet varchar(20) > 

SQL table populated with some records looks like below:
SQL
ID | Planet

 1 | Mercury

 2 | Venus

 3 | Earth

Treeview is populated with following pattern:

  • Parent node = column name
  • Parent's children = distinct column values

For our example SQL table, treeview would look like this[^].

My goal is to use treeview to create dynamic SQL `SELECT` query.

For example:

  • if user checks Mercury, my query would be something like SELECT ID from Table where Planet = 'Mercury';

  • if user checks multiple nodes, for example Mercury and Earth then my query would become SELECT ID from Table where Planet IN ( 'Mercury', 'Earth' );.

PROBLEM:

At first, I thought to simply traverse the tree when user presses "search" button, for example. Then I could determine which nodes are checked and include them in the query.

However, tree in the image above is simplified example. In my case, treeview might be much more complex.

Therefore I must create proper query when node changes state (when it becomes checked or unchecked).

MY EFFORTS TO SOLVE THIS:

First, I have decided to substitute SELECT ID from Table where Planet = 'Mercury'; with SELECT ID from Table where Planet IN ('Mercury');. This way I do not need to check whether only one node is clicked or multiple ones.

I have found NM_TVSTATEIMAGECHANGING[^] notification that might be very useful for my task ( I am willing to drop XP support if this message will solve problem in this question ).

Reading carefully its documentation, I have found important info in Community Additions section that led me to examine TVN_ITEMCHANGING[^] notification code. This also seems useful, and again, I am willing to drop XP support if this solves my problem.

At the moment of writing this question I am experimenting with various approaches but they lead me nowhere. Therefore I will not include them in this post.

I have tried Googling for solution but have found nothing ( since this is quite creative solution for implementing search query, I did not know how to form proper Google search query, to be honest ).

QUESTION:

How can I form proper SQL query, in response to node changing its checked state?

If further explanations or info is required, leave a comment. I apologize if anything is unclear, English is not my native, and I am not that experienced with WinAPI either, so please take that into consideration when reading the question.
Posted
Updated 1-Aug-15 6:20am
v3

1 solution

(The image link didn't seem to work, so I can't speak to the TreeView.)


Use a parameterized statement; there is no need to go all dynamic on this.

If you are using SQL Server, then also look into Table-Valued Parameters.

SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]


You might also look into using bitmaps for the planet IDs (never use autonumbers or identities for simple/static data such as this):

ID | Planet
 1 | Mercury
 2 | Venus
 4 | Earth
 8 | Mars
...


You can then have an enumeration on the code side and pass one bit-mapped (integer) value, e.g. value 12 means "Earth and Mars".

This has the additional value of not requiring string comparisons in the SQL.
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900