【发布时间】:2019-01-18 10:43:37
【问题描述】:
我写了一些示例代码来演示我的问题。一个绑定到 Object,另一个绑定到 DataRow:
绑定到 DataRow 示例:
namespace WindowsFormsApplication1
{
public partial class frmBindExample : Form
{
public frmBindExample()
{
InitializeComponent();
InitForm();
}
private void InitForm()
{
//;; Init the list
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id"));
dt.Columns.Add(new DataColumn("Name"));
dt.Rows.Add(new string[] { "5476", "Smith" });
dt.Rows.Add(new string[] { "5477", "Marlin" });
Label label1 = new Label() { Top = 130, Left = 10, Text = "Id of Smith is:" };
this.Controls.Add(label1);
//;; Bind two direction with TextBox.
TextBox textbox1 = new TextBox() { Top = 130, Left = 130, Width = 100 };
this.Controls.Add(textbox1);
textbox1.DataBindings.Add("Text", dt.Rows[0], "Id");
//;; The binding system respose changing property value
Button button1 = new Button() { Top = 160, Left = 10, Width = 200, Text = "Set Id=99 Directly by property" };
this.Controls.Add(button1);
button1.Click += (s, e) =>
{
dt.Rows[0]["Id"] = "99";
};
DataGridView dg = new DataGridView() { Top = 200, Left = 10 };
this.Controls.Add(dg);
dg.DataSource = dt;
}
}
}
看起来像:
如您所见,与 TextBox 的绑定在下一个示例中不起作用。但是,当我按下按钮更新字段时,数据网格会立即刷新:
好的,现在看看如果我绑定 Object 是什么 hempen:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmBindExample : Form
{
public frmBindExample()
{
InitializeComponent();
InitForm();
}
private void InitForm()
{
//;; Init the list
List<Person> lst = new List<Person>();
lst.Add(new Person() { Id = "5476", Name = "Smith" });
lst.Add(new Person() { Id = "5477", Name = "Marlin" });
Label label1 = new Label() { Top = 130, Left = 10, Text = "Id of Smith is:" };
this.Controls.Add(label1);
//;; Bind two direction with TextBox.
TextBox textbox1 = new TextBox() { Top = 130, Left = 130, Width = 100 };
this.Controls.Add(textbox1);
textbox1.DataBindings.Add("Text", lst[0], "Id");
//;; The binding system respose changing property value
Button button1 = new Button() { Top = 160, Left = 10, Width = 200, Text = "Set Id=99 Directly by property" };
this.Controls.Add(button1);
button1.Click += (s, e) =>
{
lst[0].Id = "99";
};
DataGridView dg = new DataGridView() { Top = 200, Left = 10 };
this.Controls.Add(dg);
dg.DataSource = lst;
}
}
//;; The person class can bind any his property without any extra call fo change detection
public class Person
{
public string Id { get; set;}
public string Name { get; set; }
}
}
现在,TextBox 显示我们的 Id 值。但按下设置按钮不会刷新 DataGrid 上的数据。
所以,我的问题是:
- 为什么绑定 TextBox 在第一个示例中不能正常工作?
- 确实可以说,自动(无需任何额外的 do 绑定调用)将更新从源传播到控件只发生在 DataRow 上?
【问题讨论】:
-
使用
BindingList<Person>而不是List<T>。另外,使用textbox1.DataBindings.Add("Text", lst, "Id"); -
我尝试:BindingList
lst = new BindingList ();,但这并没有改变结果。 DatagridView 仍然没有像 DataRow 版本那样刷新。 -
我没有看到代码。您可以做几件事:1) 为
TextBox.TextChanged事件添加一个事件处理程序。在处理程序方法中,调用this.dg.UpdateCellValue(0, this.dg.CurrentRow.Index);2) 创建一个BindingSource并将其DataSource设置为您的列表。在向TextBox添加Binding时,订阅Binding的Parse事件,那里调用[YourBindingSource].CurrencyManager.Refresh();。还有更多可用的方法。 -
是的。你说得对,有额外的代码我可以做到。但我发现 DataSource、Datatable 和 BindingList 类型与对象属性之间存在很大差异。第一个无需任何额外代码即可刷新 UI。请问这个结论是否正确,与对象属性的绑定必须额外的代码才能刷新。
-
不使用 BindingSource。如果将
BindingSource.DataSource设置为BindingList,然后使用BindingSource作为数据源将绑定添加到TextBox 属性,同时使用BindingSource作为DataGridView.DataSource,更新将是自动的。我建议订阅 Binding 的Parse事件,因为您可能希望在提交新值之前验证输入的值。如果您需要示例,请告诉我。
标签: c# .net winforms data-binding