Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm implementing a Gridview that can accept several different sets of results depending on the query. It's the front end for an electronic component database, and different columns will be used to display information on resistors, capacitors, etc. My intent was to hide all of the columns with data on components that weren't part of the query, and that I can do. However, it the query returns, for instance, data about resistors, then I get an error from the first column devoted to non-resistor data. The error message says that the field wasn't found. That makes sense, since the query didn't retrieve data from those other tables, and the fields don't exist in the Gridview's datasource. My question is, how can I prevent the "field not found" error? What I would like is a way to conditionally disable or remove the columns not related to my query, or handle the error so that the Gridview will display, with only the relevant columns visible. Any thoughts?

-Chris
Posted
Updated 5-Dec-11 8:34am
v2
Comments
Wonde Tadesse 5-Dec-11 19:16pm    
Tag it. WinForm, Asp.NET, WPF ...

Since the query result returns different values each time, you can enable the gridview AutoGenerateColumns property instead of defining specific columns.
 
Share this answer
 
Comments
Chris_DI 6-Dec-11 18:39pm    
using the AutoGenerateColumns would add a lot of work in formatting all of the columns programatically. I didn't say in the original post, but there's a bit over 200 possible columns. On the other hand, if I avoid performing the Eval or Bind operations by using something like:
Text=' gv.Columns[(int)Col.TypeName].Visible ? <%#Eval("TypeName") %> : "" '
then the error doesn't get thrown. Yes, it's a lot of editing, but it's fairly regular, and I may be able to automate the edit a bit.
Wonde Tadesse 6-Dec-11 18:55pm    
If you really know the columns from the query result this might be fine. But pre defining columns as static is not as such good. Since it will be difficult to maintain for the future the application. And a Qn, Are you really going to display 200 columns to gridview ?
Chris_DI 9-Dec-11 18:20pm    
In this case, the column set is implied by the query, and user preferences and permissions will determine which columns in that set are displayed. And the 200 columns won't ever all be displayed - that's the total number of possibilities that exist. For any given query, there would be a small fraction of that number available. Thanks!
Wonde Tadesse 9-Dec-11 20:14pm    
How much you are sure a user won't display all the 200 columns? Really you need to think about future maintainability!
Chris_DI 12-Dec-11 16:59pm    
100%. What columns get displayed are defined by the query, and users only get to determine visibility within the set that is so defined. If you're looking at specifications for, let's say, a transistor, then the fields associated with, again let's say, a capacitor aren't part of the choices that you see when the choice of which columns to show is presented to you.
Some suggestions:
You could add and remove the columns from the GridView alltogether (so they won't be bound to) instead of just not showing them.
You could set the binding field dynamically dependent on the query, although this is probably more work.
You could not have any design time columns at all and have them generated when you do the binding. If you do this you should make sure the default column width is ok (or somehow set them after the binding) and that the column names on the datasource are what you want the user to see too (or again, somehow change them after the binding).

Overall I'm not sure if having one grid possibly show multiple tables is such a good idea. I've seen such stuff and maintanence is a nightmare (I've got three times the columns I expect, what goes where? I change one 'view' and I break another) etc.
In one situation we had the same and then the user called and said they wanted to be able to set columns on or off themselves. No big deal you would say, except that if we showed them what columns we didn't show (so they could put those back on again) they would see a hell of a lot of columns that they didn't use or want to use at that time :)

I think easier would be to simply have three grids and hide/show the ones you want at a given time. Perhaps also not the best solution, but at least pretty easy and straightforward.

Of course I don't really know your situation, so consider this as mere advice. But you really don't have other options?
Hope it helps! :)
 
Share this answer
 
Comments
Chris_DI 6-Dec-11 18:42pm    
Making the eval/bind conditional looks like the best option - see the comment below. Thanks, this pointed me in the right direction. The situation that drives me to this is more complex than I originally described - there's over a dozen possible tables involved, and a few other issues. But this will work.
Sander Rossel 6-Dec-11 18:48pm    
Not sure what you mean or what's the problem, but glad my tips could help you out :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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