- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 DataGridView 中显示对象列表。一切正常。根据对象的属性自动将列添加到 DataGridView。
现在我更改了我在网格中显示的类以实现 ICustomTypeDescriptor。但是现在,当我将其 DataSource 设置为我的自定义对象列表时,网格不再显示任何列或行。
我猜这与以下事实有关:使用 ICustomTypeDescriptor,每个网格的每一行中显示的每个实例都可能返回一组不同的属性。
我正在实现 ICustomTypeDescriptor,这样我就可以允许用户在运行时向对象动态添加自定义属性。这些自定义属性应该通过 DataGridView 可见和可编辑。
为什么 DataGridView 看不到我的 ICustomTypeDescriptor 方法?是否有另一种方法可以动态地将属性添加到将显示在 DataGridView 中的对象?
最佳答案
DataGridView
查看元数据的列表版本;规则是……复杂的:
IListSource
, GetList()
被评估并用作数据源(在 2 处继续)ITypedList
, GetProperties()
用于获取元数据(退出)object
)索引器(即 public T this[int index]
),则 T
通过 TypeDescriptor.GetProperties(type)
用作来源:
TypeDescriptionProvider
已分配,这用于针对类型的元数据(退出)TypeDescriptor.GetProperties(list[0])
用于元数据:
ICustomTypeDescriptor
被实现,然后被使用(退出)[*]TypeDescriptionProvider
已分配,这用于针对类型的元数据(退出)[*]([*]=我不记得这两个人走了哪条路...)
如果您使用 List<T>
(或类似的),那么你遇到了“最简单的”(IMO)案例 - #3。如果您想提供自定义元数据,那么;你最好的选择是写一个 TypeDescriptionProvider
并将其与类型相关联。我可以写一个例子,但这需要一段时间(可能在火车上)...
编辑:here's使用 ITypedList
的示例;我会尝试调整它以使用 TypeDescriptionProvider
相反……
第二次编辑:使用 TypeDescriptionProvider
的完整(但最小)示例跟随;长代码警告...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
// example
static class Program {
[STAThread]
static void Main() {
PropertyBag.AddProperty("UserName", typeof(string), new DisplayNameAttribute("User Name"));
PropertyBag.AddProperty("DateOfBirth", typeof(DateTime), new DisplayNameAttribute("Date of Birth"));
BindingList<PropertyBag> list = new BindingList<PropertyBag>() {
new PropertyBag().With("UserName", "Fred").With("DateOfBirth", new DateTime(1998,12,1)),
new PropertyBag().With("UserName", "William").With("DateOfBirth", new DateTime(1997,4,23))
};
Application.Run(new Form {
Controls = {
new DataGridView { // prove it works for complex bindings
Dock = DockStyle.Fill,
DataSource = list,
ReadOnly = false, AllowUserToAddRows = true
}
},
DataBindings = {
{"Text", list, "UserName"} // prove it works for simple bindings
}
});
}
}
// PropertyBag file 1; the core bag
partial class PropertyBag : INotifyPropertyChanged {
private static PropertyDescriptorCollection props;
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
static PropertyBag() {
props = new PropertyDescriptorCollection(new PropertyDescriptor[0], true);
// init the provider; I'm avoiding TypeDescriptionProviderAttribute so that we
// can exploit the default implementation for fun and profit
TypeDescriptionProvider defaultProvider = TypeDescriptor.GetProvider(typeof(PropertyBag)),
customProvider = new PropertyBagTypeDescriptionProvider(defaultProvider);
TypeDescriptor.AddProvider(customProvider, typeof(PropertyBag));
}
private static readonly object syncLock = new object();
public static void AddProperty(string name, Type type, params Attribute[] attributes) {
lock (syncLock)
{ // append the new prop, into a *new* collection, so that downstream
// callers don't have to worry about the complexities
PropertyDescriptor[] newProps = new PropertyDescriptor[props.Count + 1];
props.CopyTo(newProps, 0);
newProps[newProps.Length - 1] = new PropertyBagPropertyDescriptor(name, type, attributes);
props = new PropertyDescriptorCollection(newProps, true);
}
}
private readonly Dictionary<string, object> values;
public PropertyBag()
{ // mainly want to enforce that we have a public parameterless ctor
values = new Dictionary<string, object>();
}
public object this[string key] {
get {
if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
object value;
values.TryGetValue(key, out value);
return value;
}
set {
if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
var prop = props[key];
if (prop == null) throw new ArgumentException("Invalid property: " + key, "key");
values[key] = value;
OnPropertyChanged(key);
}
}
internal void Reset(string key) {
values.Remove(key);
}
internal bool ShouldSerialize(string key) {
return values.ContainsKey(key);
}
}
static class PropertyBagExt
{
// cheeky fluent API to make the example code easier:
public static PropertyBag With(this PropertyBag obj, string name, object value) {
obj[name] = value;
return obj;
}
}
// PropertyBag file 2: provider / type-descriptor
partial class PropertyBag {
class PropertyBagTypeDescriptionProvider : TypeDescriptionProvider, ICustomTypeDescriptor {
readonly ICustomTypeDescriptor defaultDescriptor;
public PropertyBagTypeDescriptionProvider(TypeDescriptionProvider parent) : base(parent) {
this.defaultDescriptor = parent.GetTypeDescriptor(typeof(PropertyBag));
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) {
return this;
}
AttributeCollection ICustomTypeDescriptor.GetAttributes() {
return defaultDescriptor.GetAttributes();
}
string ICustomTypeDescriptor.GetClassName() {
return defaultDescriptor.GetClassName();
}
string ICustomTypeDescriptor.GetComponentName() {
return defaultDescriptor.GetComponentName();
}
TypeConverter ICustomTypeDescriptor.GetConverter() {
return defaultDescriptor.GetConverter();
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() {
return defaultDescriptor.GetDefaultEvent();
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() {
return defaultDescriptor.GetDefaultProperty();
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType) {
return defaultDescriptor.GetEditor(editorBaseType);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) {
return defaultDescriptor.GetEvents(attributes);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents() {
return defaultDescriptor.GetEvents();
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) {
return PropertyBag.props; // should really be filtered, but meh!
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() {
return PropertyBag.props;
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) {
return defaultDescriptor.GetPropertyOwner(pd);
}
}
}
// PropertyBag file 3: property descriptor
partial class PropertyBag {
class PropertyBagPropertyDescriptor : PropertyDescriptor {
private readonly Type type;
public PropertyBagPropertyDescriptor(string name, Type type, Attribute[] attributes)
: base(name, attributes) {
this.type = type;
}
public override object GetValue(object component) {
return ((PropertyBag)component)[Name];
}
public override void SetValue(object component, object value) {
((PropertyBag)component)[Name] = value;
}
public override void ResetValue(object component) {
((PropertyBag)component).Reset(Name);
}
public override bool CanResetValue(object component) {
return true;
}
public override bool ShouldSerializeValue(object component) {
return ((PropertyBag)component).ShouldSerialize(Name);
}
public override Type PropertyType {
get { return type; }
}
public override bool IsReadOnly {
get { return false; }
}
public override Type ComponentType {
get { return typeof(PropertyBag); }
}
}
}
关于c# - DataGridView 不显示实现 ICustomTypeDescriptor 的对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1468840/
我已经看到 Datagridview 不允许复制和粘贴多个单元格的文本,是否有一个简单的设置来启用它,或者我是否必须使用键处理程序和剪贴板数据存储来包含该功能。 用户想要在一行中复制 3 个单元格,并
我有一个 DataGridView我想限制用户只为特定列下的单元格输入数值的控件。我怎样才能在 DataGridView 中完成这种类型的验证细胞? 当我创建一个简单的文本框时是可能的,但是我如何验证
我正在尝试格式化 DataGridView,使用样式颜色等。DGV 在表单启动时加载(通过 buildGrid 方法),如构造函数代码中所示: public Report1(DataSet d
第一列中的小黑三角的官方DataGridView命名法描述是什么? 好像是标记了DataGridView.CurrentRow的位置,但是只是一个get属性,我想设置一下。 Grid with arr
我有点卡在我写的一些代码上 一个大纲是我正在从 SQL 数据库中读取一些数据,并希望将其显示在表单上的 DataGridView 中。我已经确认有数据从数据库返回,但不确定为什么没有出现。我遵循了互联
我的 DataTable从数据库中提取了三列,而我只需要将其中的两列绑定(bind)到 DataGridView .你能帮我吗? 最佳答案 这可能有用 DataSet ds = new DataSet
我有一个包含 Dataset ds 和 DataGridView dgv 的 WinForms 应用程序。 dgv 绑定(bind)到 ds。 ds 通过 Task() 更新,该 Task() 使用
我有DataGridView(dgHome),在parentForm(Home)中有一些列。子 Form(bill) 有 DataGridView(dgbill) 。我需要当我单击 dgHome 中的
我有两个具有相同列架构的 DataGridView(尽管有两个不同的 DataView 作为 DataSource - 如果这很重要)。将一行从一个数据 GridView 移动到另一个数据 GridV
基本上我有 2 个 DataGridView,我需要将行从一个复制到另一个。 到目前为止我已经尝试过: DataGridViewRowCollection tmpRowCollection = Dat
我正在尝试根据它包含的行数使我的 DataGridView 的高度自动调整。目前,我能够通过以下行完成此操作: dataGridView_SearchResults.AutoSize = true;
使用 WinForms、C# .Net 2.0 我正在向非绑定(bind) DataGridView 添加行。我想在其中一列中有一个 DataGridViewButtonColumn,在单击时删除该行
我有一个 datagridview(dataGridView1,其中包含一个 dataGridview2 列标题),其中我有一个复选框列,称为 dgvCkb,在 dataGridView1。当我在任何
对不起,如果这是一个愚蠢的问题。我在这方面很陌生。我应该如何将组合框添加到数据表,然后将其加载到数据 GridView 中?这可以做到吗?这是最好的方法吗?非常感谢有关如何执行此操作的提示和教程。先感
我正在尝试将 .txt 文件导入 DataGrid。问题是虽然代码大部分工作正常,但在导入 .txt 文件时,它会创建额外的行,例如 this ; OpenFileDialog ofd
datagridview 加载速度非常慢。我该如何优化它? datagridview有4-5千行。 我必须根据几个参数动态生成一个 datagridview。(来自数据库的数据,列数) 我必须从数据库
我怎样才能强制DataGridView.CellValueChanged DataGridViewCell.Value 立即引发事件(并将更改实际提交给 ComboBox 属性)单元格中的编辑控件更改
我有一个通过数据集从数据库中获取数据的功能 public DataSet getDataSet(string query) { DataSet ds = new DataSet()
客户希望能够键入一个字母并让系统滚动 DataGridView,这样具有与字母匹配的单元格的第一行将滚动到 DataGridView 的顶部 任何建议将不胜感激 最佳答案 我不确定您将如何有效地查找与
好吧,我有一个 DataGridView,用户可以在其中单击列标题进行排序。当他们在应用排序时添加新行时,直到验证该行时才会创建记录(在退出 newRow 之前他们无法执行此操作)。排序后如何才能选择
我是一名优秀的程序员,十分优秀!