解决Gridview列错误-找不到字段 [英] Resolve a Gridview column error - field not found

查看:61
本文介绍了解决Gridview列错误-找不到字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个Gridview,它可以根据查询接受几种不同的结果集.它是电子元件数据库的前端,并且将使用不同的列来显示有关电阻器,电容器等的信息.我的意图是隐藏所有列,其中包含不属于该组件的元件的数据.查询,而我可以做到.但是,如果查询返回例如有关电阻器的数据,则从第一列专用于非电阻器数据的错误中得到了错误.错误消息指出找不到该字段.这是有道理的,因为查询没有从那些其他表中检索数据,并且这些字段在Gridview的数据源中不存在.我的问题是,如何防止出现找不到字段"错误?我想要的是一种有条件地禁用或删除与我的查询无关的列或处理错误的方法,以便显示Gridview且仅显示相关列.有什么想法吗?

-Chris

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

推荐答案

由于查询结果每次都返回不同的值,因此可以启用gridview AutoGenerateColumns属性,而不用定义特定的列.
Since the query result returns different values each time, you can enable the gridview AutoGenerateColumns property instead of defining specific columns.


一些建议:
您可以一起从GridView中添加和删除列(这样它们就不会被绑定),而不仅仅是不显示它们.
您可以根据查询动态设置绑定字段,尽管这可能需要更多工作.
您根本没有任何设计时列,并且在进行绑定时会生成它们.如果执行此操作,则应确保默认列宽是确定的(或在绑定后以某种方式设置它们),并且数据源上的列名也是您希望用户看到的(或再次在绑定后以某种方式更改它们) ).

总的来说,我不确定是否有一个网格可以显示多个表是一个好主意.我看到过这样的东西,维护是一场噩梦(我的栏目是我预期的三倍,去哪儿?我更改一个视图",然后破坏另一个))等.
在一种情况下,我们遇到了相同的情况,然后用户打电话说,他们希望能够自己打开或关闭列.您会说没什么大不了的,除了如果我们向他们展示了我们不显示的列(以便他们可以再次将其放回去),他们会看到很多他们没有使用或想要的列的地狱当时使用的:)

我认为更简单的方法是只具有三个网格并在给定时间隐藏/显示您想要的网格.也许也不是最好的解决方案,但至少相当简单明了.

当然,我并不真正了解您的情况,因此请仅将此作为建议.但是您真的没有其他选择吗?
希望能帮助到你! :)
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! :)


这篇关于解决Gridview列错误-找不到字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆