Tag: bindinglist

c#BindingList只读错误

我一直在努力寻找问题的可能原因,但我似乎无法找到它: 错误: System.NotSupportedException:Collection是只读的。 System.ThrowHelper.ThrowNotSupportedException(ExceptionResource资源)位于C:\ Users \ aRJiJon \ Documents \ Visual中WindowsFormsApplication1.Form1.Btn_CSVSEATINGPLAN_Click(Object sender,EventArgs e)的System.Collections.ObjectModel.Collection`1.Add(T item) Studio 2010 \ Projects \ WindowsFormsApplication_1 \ WindowsFormsApplication1 \ Form1.cs:第72行 this.dataGridView1.DefaultCellStyle.Font = new Font(“Tahoma”, 11); bindingCSVSP.DataSource = CSV_Seating_Plan; bindingCSVSP.AllowNew = true; CSV_Seating_Plan.AllowNew = true; CSV_Seating_Plan.AllowRemove = true; CSV_Seating_Plan.RaiseListChangedEvents = true; CSV_Seating_Plan.AllowEdit = true; this.dataGridView1.DataSource = bindingCSVSP; this.btn_Process_SP.Enabled = true; CSV_Seating_Plan.Add(new csv_SeatingPlan()); […]

使用编程排序绑定到DataGridView的可排序BindingList

我已经在http://msdn.microsoft.com/en-us/library/aa480736.aspx上实现了SortableSearchableList类,并为它添加了一个Sort方法,如下所示: public void Sort(PropertyDescriptor prop, ListSortDirection direction) { ApplySortCore(prop, direction); } 这个类在通过单击任何列标题对DataGridView进行排序时起作用,但是我需要能够以编程方式调用指定列的Sort方法(在本例中使用sortButton控件)。 我在网上找到的几个代码示例建议获取列的PropertyDescriptor并将其传递给ApplySortCore方法。 我还没有那个工作。 我可以获取DataGridView或SortableSearchableList的PropertyDescriptorCollection属性,但似乎无法获取Find方法来获取指定列/成员的PropertyDescriptor。 这是我的其余代码: namespace SortableBindingListTest { public partial class Form1 : Form { private SortableSearchableList alarms = new SortableSearchableList(); public Form1() { InitializeComponent(); alarms.Add(new Tags(“some text”, “1”)); alarms.Add(new Tags(“more text”, “2”)); alarms.Add(new Tags(“another one”, “3”)); dataGridView1.AutoGenerateColumns = false; dataGridView1.AllowUserToAddRows = true; dataGridView1.EditMode […]

创建可绑定到DataGrid的自定义集合

我在一家建筑公司工作,我正在创建一个3D建模程序的插件,以协助设计。 我有一个Building类和一个Floor类。 该建筑包含对FloorList楼层集合的引用。 我正在试图弄清楚FloorList集合的基础,这样我就可以最大限度地减少创建界面来编辑集合所需的工作量。 Floor系列代表一系列相互堆叠的建筑楼层。 每个楼层都有一个读写的Floor.Height属性,以及一个只读的Floor.Elevation属性,并通过总计当前楼层以下的楼层高度来设置。 因此,无论何时在集合中添加,删除,移动或更改Floor,都需要更新Floor.Elevation属性。 另外,我想创建一个用于编辑此集合的UI。 我正在考虑使用DataGrid控件,其中列出每个Floor的高度和其他属性作为控件的一行。 用户应该能够使用控件添加,删除和重新排序楼层。 我希望将其设置为尽可能简单灵活。 这意味着我只想简单地将Floors集合绑定到DataGrid,并根据Floor类的属性填充DataGrid的列。 如果可能的话,我希望能够利用DataGrid控件的内置添加/删除UI界面,而不必在我的集合和DataGrid之间设置一堆事件关系。 为了使将来进一步复杂化,我需要能够允许用户动态地将自定义属性添加到我希望他们能够在DataGrid中查看和编辑的Floors 。 我想我最终会通过让Floor类实现IExtenderProvider来实现这一点。 所以最终DataGrid看起来像这样: 初始属性未来自定义用户属性 高度提升程序类型UnitType UnitCount 15’70’住宅豪宅5 15’55’住宅豪宅5 15’40’住宅预算10 20’20’零售不适用2 20’0’零售不适用3 我现在的问题是我应该将FloorList集合作为基础来实现这一function? 我正在考虑的选项如下。 1)inheritance自列表(楼层) 诸如添加/删除之类的方法不是玻璃的,因此我不能覆盖它们来更新高程 2)实施IList(楼层) OnChange事件不是内置的,所以如果列表更改,DataGrid将不会更新(我认为?) 我认为这可能是最好的选择,但我需要做什么来确保对FloorList集合或DataGrid的更改彼此同步? 3)inheritance自BindingList(Floor) 添加/删除等方法不是虚拟的,因此我无法修改它们来更新楼层高程。 4)实现IBindingList IBindinglist不是通用的,我只希望我的集合包含Floor对象

为什么BindingList或ObservableCollection这样的类不是线程安全的?

一次又一次我发现自己必须编写BindingList和ObservableCollection的线程安全版本,因为当绑定到UI时,这些控件不能从多个线程更改。 我想要了解的是为什么会出现这种情况 – 这是设计错误还是故意这种行为?

使用SortableBindingList – DataGridView不会自动对更改进行排序

我正在构建一个Windows窗体应用程序,它显示一个自定义类Record对象,并按照它们在我的SortableBindingList record_list SortableBindingList 。 当我启动程序时,为了测试,我已经将一些“虚拟”记录加载到此列表中。 SortableBindingList已从此处获取 。 public partial class Form1 : Form { public SortableBindingList record_list = new SortableBindingList { }; public static DataGridViewCellStyle style = new DataGridViewCellStyle(); public Form1() { InitializeComponent(); dataGridView.DataSource = record_list; FillData(); //Temporary function to insert dummy data for demo. dataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.cell_formatting); this.Controls.Add(dataGridView); this.dataGridView.RowHeadersVisible = false; this.dataGridView.Sort(this.dataGridView.Columns[“UserName”], ListSortDirection.Ascending); […]

将IList 转换为BindingList

如何将IList列表转换为BindingList ?

获取Enum 值描述

我有我的enumHelper类包含这些: public static IList GetValues() { IList list = new List(); foreach (object value in Enum.GetValues(typeof(T))) { list.Add((T)value); } return list; } 和 public static string Description(Enum value) { Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute)); if (DescAttribute == null) return value.ToString(); else return ((DescriptionAttribute)DescAttribute).Description; } 我的枚举是这样的: public enum OutputType { File, [Description(“Data Table”)] DataTable } 到现在为止还挺好。 […]

我需要一个BindingSource和一个用于WinForms DataBinding的BindingList吗?

我想在Windows窗体应用程序中显示DataGridView中的人员列表。 我希望我的服务层返回Person对象列表(例如, IList )。 我希望列表中的更改能够反映在DataGridView ,反之亦然。 我的理解是使用BindingSource有助于使用DataGridView 。 我的问题是双向数据绑定工作,我需要: //pseudo code BindingSource.DataSource = IBindingList 或者我可以这样做: BindingSource.DataSource = IList 有什么不同? 如果我对列表进行了更改,那么DataGridView会以哪种方式更新? 如果我必须使用BindingList ,从我的服务层返回一个BindingList似乎有点不可思议(因为创建一个依赖),有没有办法解决这个问题? 微软谈到BindingList (在备注部分) http://msdn.microsoft.com/en-us/library/ms132679.aspx : “但是,典型的解决方案程序员将使用提供数据绑定function的类,例如BindingSource ,而不是直接使用BindingList 。”

从BindingList 派生的类的公共字段/属性不会序列化

我正在尝试序列化一个派生自BindingList(Floor)的类 ,其中Floor是一个只包含属性Floor.Height的简单类 这是我class级的简化版 [Serializable] [XmlRoot(ElementName = “CustomBindingList”)] public class CustomBindingList:BindingList { [XmlAttribute(“publicField”)] public string publicField; private string privateField; [XmlAttribute(“PublicProperty”)] public string PublicProperty { get { return privateField; } set { privateField = value; } } } 我将使用以下代码序列化CustomBindingList的实例。 XmlSerializer ser = new XmlSerializer(typeof(CustomBindingList)); StringWriter sw = new StringWriter(); CustomBindingList cLIst = new CustomBindingList(); Floor fl; fl […]

在BindingList的ItemChanging事件中获取已删除的项目

我在我的应用程序中使用绑定列表以及ItemChanged事件。 有什么办法可以知道ItemChanged事件中以前的属性值。 目前我正在添加一个名为’OldValue’的独立属性来实现此目的。 有没有办法知道项目更改事件中已删除的项目。 我无法找到任何方法来知道哪个项目已从列表中删除。