Tag: bindingsource

如何从绑定到List 或匿名类型的绑定源获取正确的映射名称,以便在DataGridTableStyle上使用?

我正在尝试创建一个DataGridTableStyle对象,以便我可以控制DataGrid的列宽。 我创建了一个绑定到List的BindingSource对象。 实际上,它通过以下方式绑定到通过Linq创建的匿名类型列表(为了清楚我正在做的事情,变量名称已更改): List myList = new List(someCapacity); . …populate the list with query from database… . var query = from i in myList select new { i.FieldA, i.FieldB, i.FieldC }; myBindingSource.DataSource = query; myDataGrid.DataSource = myBindingSource; 然后我创建一个DataGridTableStyle对象并将其添加到datagrid。 但是,它永远不会应用我设置的表样式属性,因为我似乎无法设置正确的myDataGridTableStyle.MappingName属性。 我在谷歌搜索了大约1/2个小时,并且在一堆不同的论坛中一直看到相同问题的链接(字面意思是相同的文字,就像有人刚刚复制并粘贴了这个问题……我讨厌那个……) 。 无论如何,没有任何建议有效,就像那个人在所有其他网站上说的那样。 那么有谁知道我需要设置MappingName属性,以使我的TableStyle实际上正常工作? 我在哪里可以从中获取名称? (它不能为空……只适用于绑定到DataTable或SqlCeResultSet等的BindingSource)。 我认为这可能是一个问题,我使用Linq创建一个匿名的,更专业的对象版本,只有我需要的字段。 我应该尝试将BindingSource直接绑定到List对象吗? 或者甚至可以将DataGrid直接绑定到List对象并完全跳过绑定源。 谢谢 PS – C#,Compact Framework v3.5 更新: […]

在WinForms中更新BindingSource不会更新数据源集合

我想在Windows窗体应用程序的DataGridView中显示自定义集合。 此自定义集合实现ICollection和IEnumerable 。 我已经设置了一个BindingSource ,使用该集合作为.DataSource属性。 DataGridView设置为使用我的BindingSource作为DataSource。 当我使用BindingSource.Add()方法向集合中添加新项时, DataGridView使用新项正确更新。 另一方面, BindingSource DataSource不会: MyCustomCollection myCollection = new MyCustomCollection(); myCollection.Add(myCustomObject1); myCollection.Add(myCustomObject2); myBindingSource.DataSource(myCollection); myBindingSource.Add(myCustomObject3); 在上面的代码中,myBindingSource的内部List包含正确数量的记录(3),而DataGridView也包含三个记录,但myCollection只包含两个记录。 我知道更改底层的myCollection不会更新BindingSource或DataGridView ,因为它不是BindingList ,但我的印象是直接更新BindingSource会确保myCollection同时更新。 有没有办法使用不是BindingList的集合,并在直接与BindingSource交互时更新它? 更新 :我在所有部分(Collection,BindingSource,DataGridView)上更新数据的一种方法如下: myCollection.Add(myCustomObject3); myBindingSource.DataSource = null; myBindingSource.DataSource = myCollection; 我很确定有更好的方法可以解决这个问题,但这是我生成预期结果的唯一方法。

C#WinForms BindingList和DataGridView – 禁止编辑可以防止创建新行? 我该如何解决这个问题?

关于我使用带有BindingList的DataGridView,我是禁用编辑当前行,但允许添加新行。 我遇到的问题是,当我不允许编辑时,这似乎阻止了添加新行项,因为当您将表放入此新行的单元格时,它似乎不允许编辑??? 知道怎么解决这个问题吗? 我的代码部分如下: BindingSource bs = new BindingSource(); bList = new BindingList(); bList.AllowNew = true; bList.AllowEdit = false; // Fill bList with Customers bList.Add(new Customer(“Ted”)); bList.Add(new Customer(“Greg”)); bList.Add(new Customer(“John”)); bs.DataSource = bList; dataGridView1.DataSource = bs; 谢谢

图像数据绑定到pictureBox

我试图通过两种方法将数据集“data_set”中数据表“Applicant’s Details”中的“Applicant’s Image”列中的图像绑定。 但是当我运行表单应用程序时,我看不到图片框中显示的图像“imgusr”。 我的绑定源名称是“bindSource”。 假设data_set正确地检索了所有内容,那么图像没有加载到图片框“imgusr”会有什么问题? 另外,sizeMode的picturebox属性为“zoom”。 private void Update_Load(object sender, EventArgs e){ data_set = blobj.srcforVU(); bindSource.DataSource = data_set; bindSource.DataMember = “Applicant’s Details”; lbidvalue.DataBindings.Add(new Binding(“Text”, bindSource, “Applicant’s ID”, false)); //method 1 //Binding binding = new Binding(“Image”, bindSource, “Applicant’s Image”, true, DataSourceUpdateMode.OnPropertyChanged); //binding.Format += new ConvertEventHandler(binding_Format); //imgusr.DataBindings.Add(binding); //method 2 imgusr.DataBindings.Add(new Binding(“Image”, bindSource, “Applicant’s Image”, true)); tbfname.DataBindings.Add(new […]

当DataSource是BindingList时过滤BindingSource

我从excel表中读取并为BindingList写了这个,在Form_Load中,这被设置为一个DataSource作为BindingSource: bd = new BindingSource(); //instance of BindingSource bd.DataSource = ExcelOPS.LerExcel(); //LerExcel() method return a BindingList gvFiltro.DataSource = bd; //set a DataGridView named gvFiltro DataSource property bindNav.BindingSource = bd; //set a BindingNavigator source 这个工作很好! 我打算为这个DataGridView gvFiltro创建一个combobox作为filter,所以在combobox的SelectedIndexChanged事件中,我试试这个: this.gvFiltro.DataSource = null; bd.Filter = string.Format(“TAG_FAZENDA like ‘%{0}%'”, cbTagFaz.Text); gvFiltro.DataSource = bd; gvFiltro.Update(); gvFiltro.Refresh(); bindNav.BindingSource = bd; bindNav.Update(); bindNav.Refresh(); […]

C#DataGridView.DataSource使用BindingSource与之间的区别

我发现我可以直接将DataGridView.DataSource设置为DataTable而不使用它们之间的BindingSource,这就是我到目前为止所使用的所有教程。 那两个人之间有什么区别?

为什么Browsable属性使属性不可绑定?

我正在尝试使用System.Windows.Forms.PropertyGrid 。 要使属性在此网格中不可见,应将BrowsableAttribute设置为false 。 但添加此属性会使该属性无法绑定。 示例:创建一个新的Windows窗体项目,并将TextBox和PropertyGrid拖放到Form1 。 使用下面的代码, TextBox的宽度不会绑定到Data.Width : public partial class Form1 : Form { public Form1() { InitializeComponent(); Data data = new Data(); data.Text = “qwe”; data.Width = 500; BindingSource bindingSource = new BindingSource(); bindingSource.Add(data); textBox1.DataBindings.Add(“Text”, bindingSource, “Text”, true, DataSourceUpdateMode.OnPropertyChanged); textBox1.DataBindings.Add(“Width”, bindingSource, “Width”, true, DataSourceUpdateMode.OnPropertyChanged); propertyGrid1.SelectedObject = data; } } 数据类的代码是: public […]

我需要一个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 。”

通过单独的任务更新BindingSource中的元素

我有一个class,比如说人,有一个Id和一个名字。 该类正确实现了INotifyPropertyChanged 另外:有人要求上课人。 我真正的问题是一个更复杂的课程,我把它简化为一个相当简单的POCO,以确定它不是因为我的课程。 本来: public class Person { public int Id {get; set;} public string Name {get; set;} } 对于更新,它需要实现INofityChanged。 完整的代码就在这个问题的最后 StackOverflow:如何正确实现INotifyPropertyChanged 我有一个System.Windows.Forms.Form 此表单有一个BindingSource。 绑定源的DataSource属性设置为我的类Person 我有一个绑定到BindingSource的DataGridView 我已经为绑定源添加了几个Person实例 添加的人员被正确显示。 如果我以编程方式更改bindingsource中的Person,则会正确显示更改的值。 到现在为止还挺好。 如果在单独的线程中更改Person,则会出现问题。 我经常收到带有消息的InvalidOperationException BindingSource不能是自己的数据源。 不要将DataSource和DataMember属性设置为引用BindingSource的值。 我想这与更新是在一个等待的异步任务中完成的事实有关。 我知道在更新用户界面项之前,您应该检查InvokeRequired是否相应地采取行动。 private void OnGuiItemChanged() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(() => { OnGuiItemChanged(); })); } else { … // […]

选择新添加的Row – DataGridView和BindingSource

我正在向绑定到DataGridView的BindingSource添加一个新的Row source.AddNew(); 在此之后,使用BindingSource获取新添加的行,当它排序时返回DataGridView中的下一行。 ROW “A” ROW “B” <- myBindingSource.AddNew(); ROW "C" myBindingSource.Current给出ROW“C”。 (它成为DataGridView中的选定行) 我需要这个,因为我想只更新新添加的行 DataRowView drv = (DataRowView)myBindingSource.Current; myTableAdapter.Update(drv.Row); 而不是整个表。 myTableAdapter.Update(myDataSet.myTable); 而且,我想在插入后在DataGridView中选择这个新添加的行。 有可能吗?