简体   繁体   English

DataGridView数据源

[英]DataGridView datasource

I have a DataGridView and a list of some objects which populated from a SQL table. 我有一个DataGridView和一个从SQL表填充的一些对象的列表。 There are two ways I used to bind the list to the grid. 我用两种方法将列表绑定到网格。

1.Using the list directly to the datasource 1.将列表直接用于数据源

grdSomeList.DataSource = GetListSomeObjects();

2.Using through a binding source 2.使用绑定源

_bsSomeList = new BindingSource();
_bsSomeList .DataSource = GetListSomeObjects();
grdSomeList.DataSource = _bsSomeList ;

What is the best practice to bind the data source? 绑定数据源的最佳做法是什么? Is there are some specific reasons behind these two? 这两个背后是否有一些具体原因?

Use the first way if its one-way binding. 如果是单向绑定,请使用第一种方式。

Use the second way if its two-way binding, meaning when users change DataGridView Cells the changes will be kept/persisted in the GetListSomeObjects() datasource. 如果是双向绑定,则使用第二种方式,这意味着当用户更改DataGridView Cells时,更改将保留/保留在GetListSomeObjects()数据源中。

You haven't specified if this is WPF, Winforms, Web but you can read up more on BindingSource's and One, Two & etc Way Binding : 您尚未指定这是否是WPF,Winforms,Web,但您可以阅读有关BindingSourceOne,Two等等方式绑定的更多信息

TwoWay 双向

Causes changes to either the source property or the target property to automatically update the other. 导致更改源属性或目标属性以自动更新另一个属性。 This type of binding is appropriate for editable forms or other fully-interactive UI scenarios. 此类绑定适用于可编辑表单或其他完全交互式UI方案。

OneWay 单程

Updates the binding target (target) property when the binding source (source) changes. 绑定源(源)更改时更新绑定目标(目标)属性。 This type of binding is appropriate if the control being bound is implicitly read-only. 如果绑定的控件是隐式只读的,则这种类型的绑定是合适的。 For instance, you may bind to a source such as a stock ticker. 例如,您可以绑定到股票代码等来源。 Or perhaps your target property has no control interface provided for making changes, such as a data-bound background color of a table. 或者,您的目标属性可能没有为进行更改而提供的控制接口,例如表的数据绑定背景颜色。 If there is no need to monitor the changes of the target property, using the OneWay binding mode avoids the overhead of the TwoWay binding mode. 如果不需要监视目标属性的更改,则使用OneWay绑定模式可以避免TwoWay绑定模式的开销。

OneTime 一度

Updates the binding target when the application starts or when the data context changes. 在应用程序启动时或数据上下文更改时更新绑定目标。 This type of binding is appropriate if you are using data where either a snapshot of the current state is appropriate to use or the data is truly static. 如果您使用的数据适用于当前状态的快照或数据是真正静态的,则此类型的绑定是合适的。 This type of binding is also useful if you want to initialize your target property with some value from a source property and the data context is not known in advance. 如果要使用源属性中的某个值初始化目标属性并且事先不知道数据上下文,则此类型的绑定也很有用。 This is essentially a simpler form of OneWay binding that provides better performance in cases where the source value does not change. 这实际上是OneWay绑定的一种更简单的形式,在源值不变的情况下提供更好的性能。

OneWayToSource OneWayToSource

Updates the source property when the target property changes. 目标属性更改时更新source属性。 Default Uses the default Mode value of the binding target. 默认值使用绑定目标的默认模式值。 The default value varies for each dependency property. 每个依赖项属性的默认值都不同。 In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings. 通常,用户可编辑的控件属性(例如文本框和复选框的属性)默认为双向绑定,而大多数其他属性默认为单向绑定。 A programmatic way to determine whether a dependency property binds one-way or two-way by default is to get the property metadata of the property using GetMetadata and then check the Boolean value of the BindsTwoWayByDefault property. 确定依赖项属性是默认绑定单向还是双向的一种编程方法是使用GetMetadata获取属性的属性元数据,然后检查BindsTwoWayByDefault属性的布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM