- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我仍然困惑于如何在不显式触发 DataGridView.Update()
的情况下更改 DataSource 的内容时自动更新 DataGridView。 . DataTable
之间似乎没有任何区别, List
, BindingList
作为(直接)DataSource 和作为(间接)DataSource 附加 BindingSource
它使用前者中的任何一个作为数据源。
我实际为此使用的 DataGridView 是不可编辑的,只显示由相应实体代码更新的条目。我最后一次尝试是使用 BindingSource
使用 BindingList
并在代码中操作 BindingSource 的内容。
我这里省略了一些方法,对基本问题没有起到作用。
表格:
private void FormLog_Load(object sender, EventArgs e) {
...
dgvLog.DataSource = Log.Current.SourceEntries;
...
}
private void ClearLog() {
Log.Current.RemoveAll();
}
public void UpdateDataSource() {
dgvLog.Update();
}
实体(单例类):
public class LogEntry {
public int ID { get; set; }
public string DateTime { get; set; }
public string Type { get; set; }
public string Event { get; set; }
public string Details { get; set; }
}
public class Log {
public BindingList<LogEntry> Entries { get; set; }
public BindingSource SourceEntries { get; set; }
public Log() {
Entries = new BindingList<LogEntry>();
SourceEntries = new BindingSource() { DataSource = Entries };
ReadAll();
}
public void Add(string type, string logEvent, string details = "") {
LogEntry entry = MapToDB(new LogEntry() {
Type = type,
Event = logEvent,
Details = details
});
DB.Write(QueryAdd(entry));
SourceEntries.Add(entry);
if (Config.Current.GetForm("Log") != null)
((FormLog)Config.Current.GetForm("Log")).UpdateDataSource();
}
public void ReadAll() {
for (int i = SourceEntries.Count - 1; i >= 0; i--) {
SourceEntries.RemoveAt(i);
}
DataTable dt = DB.Read(QueryReadAll());
if (dt != null) {
foreach (DataRow row in dt.Rows) {
SourceEntries.Add(MapToList(row));
}
}
if (Config.Current.GetForm("Log") != null)
((FormLog)Config.Current.GetForm("Log")).UpdateDataSource();
}
public void RemoveAll() {
DB.Write(QueryRemoveAll());
for (int i = SourceEntries.Count - 1; i >= 0; i--) {
SourceEntries.RemoveAt(i);
}
Add("I", "Log cleared");
}
只有当我调用 UpdateSource()
时,这才有效但这叫dgvLog.Update()
通过在我想避免的另一个单例类中使用自己编写的 FormStack。当然,可以简单地调用 dgvLog.Update()
在表格本身内,尤其是。对于这个日志示例,很明显,当显示 DataGridView 的表单仍在后台打开时,从另一个表单更新数据/在另一个表单内更新数据时,这没有帮助。
此外,由于没有区别(使用 DataTable 或 List 等与是否使用 BindingSource 之间),我想知道 BindingList 和 BindingSource 的好处/目的是什么:
这是正确的方法还是我遗漏了什么!?
顺便说一下,我使用的是 .NET v4.5.2。
最佳答案
It seems there is no difference at all between DataTable, List, BindingList as (direct) DataSource and as (indirect) DataSource with an additional BindingSource which uses any of the former as DataSource.
BindingSource 有一些用途
works but only when I call UpdateSource() which calls dgvLog.Update() by using a selfwritten FormStack in another singleton class which I would like to avoid. Of course, one could simply call dgvLog.Update() within the form itself but, esp. with this log example, it is obvious that this does not help when updating data from/within another form while the form that displays the DataGridView is still opened in the background.
Datagridview.Update() 与需要它的控件的重绘区域有关;它与提交对基础数据模型的更改无关。也许您需要 EndEdit 来完成对当前行的编辑操作并将它们提交到底层数据存储。当您单击网格中的不同行时也会发生这种情况。 Bindingsource 也有一个 EndEdit 方法。大多数情况下,您不需要自己调用这些方法
要在表单之间共享数据,请传递存储数据的数据表,并通过第二个表单中的绑定(bind)源将其绑定(bind)
Also, as there is no difference (between using DataTable or List, etc. and BindingSource or not) I wonder what the benefit/purpose of BindingList and BindingSource are:
DataTable 是DataRow 的集合。 DataRow 的核心是一个对象数组。结束
绑定(bind)列表是任何你想要的列表,比如你的自定义类 Person。可以说最终更有用,但它是在比较苹果和橙子。相反,如果您打开数据集设计器,那么您可以指定具有命名类型列的表。在这方面,它与编写您自己的类并没有太大区别(尽管它在短时间内编写了大量好的代码。我有时将它们用作数据模型
关于C#:DataGridView 数据源更新 - DataTable、List、BindingList 和 BindingSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68249519/
我注意到这两种在 winforms 中实现数据绑定(bind)的方法。但是,我想知道其中哪一种更受欢迎(在整体性能方面,例如设计时间,效率?据我所知,这两种是: BindingSource 作为 Bi
是什么让我使用这样的东西: DataGridView dgvDocuments = new DataGridView(); BindingSource bindingSource = new Bind
我有一个带有数据 GridView 和一些绑定(bind)到绑定(bind)源的文本框的表单。表单上有一个新建按钮和保存按钮。我希望用户 单击“新建”按钮,这将导致网格转到新行并且文本框变为空白(因为
我想我很了解 BindingSource 类的作用——即在数据源和 UI 控件之间提供一层间接。它实现了 IBindingList 接口(interface),因此还提供了对排序的支持。而且我已经使用
我无法获取当前行值。我该怎么办? bindingSource1.DataSource = LData; (LData is Generic List) public DataRow currentRo
绑定(bind)源代码: Component.DataSource = new BindingSource(yourSource, ????); 我看到的所有示例都遵循这种模式,而不是 ????,我总
是否可以在多个列上使用 BindingSource 的 Find 方法? 例如,假设我有一个显示当前宠物的 GridView ;两个组合框,cboPetType 和 cboGender;以及一个用于根
我正在尝试绑定(bind)到 Linq-To-SQL 查询的结果,我已将其设置为填充图形。 该图沿着 X 轴的日期线和 Y 轴的货币线。有两个系列,一个是进钱的,一个是出钱的。 到目前为止还很简单。
我遇到的问题是过滤器不接受日期时间的时间部分。 为了清楚起见, 这有效。 " AND NextWorkDate <= #" + DateTime.Now.AddDays(1).ToString("dd
我的winform中有很多bindingsource,我想动态改变我的gridview的dataSource,所以我想通过名称获取bindingSource。我找到了以下代码来从我的 winform
我想根据日期从数据库中过滤值。 数据库中的日期包含如下值:2008-12-28 18:00:00。我的类(class)有一个 DateTime 变量,具体取决于我要过滤的内容。理想情况下它会像这样工作
为了解释这个问题,我把所有需要的东西都放到了一个小的示例应用程序中,希望能解释这个问题。我真的试图尽可能减少所有内容,但在我的实际应用程序中,这些不同的 Actor 彼此不认识,也不应该。因此,像“在
我有一个 C# Windows 窗体项目,其中包含一个包含 2 个列表框和一个按钮的窗体。 在 FormLoad 上,左侧 ListBox 填充了一个列表(大约 1800 项),其中包含有关证券(ID
使用绑定(bind)到绑定(bind)源控件的 datagridview 绑定(bind)到 LINQ to SQL类,我想知道如何将 bindingSource 定位到特定记录,也就是说,当我在文本
我有一个绑定(bind)到 DataTable 的 BindingSource。 我使用 BS 过滤器并希望使用 Bindingsource 迭代数据表的过滤数据集。 我知道我可以执行 MoveFir
这是我正在处理的表单部分: 以下代码使用 bindingSource 将 BindingNavigator 链接到数据集。我可以使用此绑定(bind)源将两个文本框连接到数据吗? 我是否只需要使用文本
我正在考虑使用数据绑定(bind) - 最简单的事情似乎是使用 BindingSource 来包装我的数据对象。 但是,虽然 CurrentItemChanged 事件会告诉我属性何时发生更改,但它不
这是我正在处理的表单部分: 以下代码使用 bindingSource 将 BindingNavigator 链接到数据集。我可以使用此绑定(bind)源将两个文本框连接到数据吗? 我是否只需要使用文本
来自微软:“BindingSource.ListChanged 事件在基础列表更改或列表中的项目更改时发生”。 但在我的示例中,事件会在每次位置更改时触发。表单有一个 UserControl、一个 B
我有一个只有 1 个对象作为数据源的 BindingSource。 我绑定(bind)了数据源上的一些值。这非常有效。 但是。 当我这样做时: bindingSource.DataSource = n
我是一名优秀的程序员,十分优秀!