自定义DataGridView窗体设计器自动生成列问题 [英] Custom DataGridView Forms Designer Auto-Generating Columns Problem

查看:71
本文介绍了自定义DataGridView窗体设计器自动生成列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有,

我没有找到任何关于这个问题的参考,所以我想我会尝试提交你的集体智慧。

我要问:
我在自定义表单控件库中有一个C#类,它从DataGridView继承(间接)。此自定义DataGridView旨在允许编辑具有许多复杂关系的特定类型的业务对象(即,显示显示通过桥接表等相关的对象的Name属性的ComboBox)。由于它的目的是相当有限的,我使用VS2008设计器明确指定列并设置AutoGenerateColumns = false(当然希望我知道为什么这个属性仅在运行时可用)。

一切正常,直到我打开包含WinForm的设计者。此时,IDE似乎会自动生成自定义DataGridView中所有(已指定)列的副本。由于某些原始列(通常是DataGridViewComboBoxColumns)需要选项的运行时数据绑定,因此应用程序会在将网格绑定到"无效组合框值"时爆炸。错误等(因为组合框列的副本没有填充任何项目。)

我可以通过手动编辑表单的< 表单名称>暂时解决此问题。 .designer.cs文件,但是,一旦我重新打开表单的设计器,我的所有手工编辑都会被覆盖,我回到我开始的地方。

有谁知道如何压制自动 - 由表单的设计者生成列?

提前感谢...

解决方案

您是否将 AutoGenerateColumns 设置为false在派生类的构造函数中?这种方法的问题在于没有什么能阻止某人重置它。另一种方法是对 OnDataSourceChanged / OnDataMemberChanged 事件做出反应,并始终将该属性重置为false,以确保它不会以编程方式设置。我相信这仍然是网格尚未重新生成列的过程中的早期。

另一种选择,虽然我犹豫不决,但是要用新的方法重新定义 AutoGenerateColumns 修饰符并忽略设置请求。更重要的是,总是从getter返回false。

Michael Taylor - 1/16/09
http://p3net.mvps。有机结果

Greetings All,

I've had no luck finding any reference to this problem so I thought I'd try submitting to your collective wisdom.

To whit:
I've got a C# class in a custom forms control library that inherits (indirectly) from DataGridView. This custom DataGridView is designed to allow for editing of a specific type of business object with a number of complex relationships (i.e., to show ComboBoxes displaying the Name property of objects related via bridge tables and the like). Since it's purpose is fairly constrained, I used the VS2008 designer to explicitly specify the columns and set AutoGenerateColumns = false (sure wish I knew why this property is only available at runtime).

All works fine, until I open the designer for the containing WinForm. At that point, the IDE seems to auto-generate copies of all of the (already-specified) columns in the custom DataGridView. Since some of the original columns (the DataGridViewComboBoxColumns, usually) require run-time data binding of options, the app blows up upon binding the grid with "Invalid Combo Box value" errors and the like (since the copies of the combo box columns have no items populated).

I can fix this, temporarily, by manually editing the form's <form name>.designer.cs file, but, as soon as I re-open the form's designer, all my hand-editing is overwritten and I'm back where I began.

Does anyone know how to suppress the auto-generation of columns by the form's designer?

Thanks in advance...

解决方案

Are you setting AutoGenerateColumns to false in the constructor for your derived class?  The problem with this approach is that nothing prevents someone from resetting it.  An alternative might be to react to the OnDataSourceChanged/OnDataMemberChanged event and always reset the property to false to ensure it doesn't get set programmatically.  I believe this is still early enough in the process that the grid hasn't regenerated the columns yet.

An alternative, although I'm hesitant, is to redefine AutoGenerateColumns with the new modifier and ignore set requests.  More importantly always return false from the getter.

Michael Taylor - 1/16/09
http://p3net.mvps.org


这篇关于自定义DataGridView窗体设计器自动生成列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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