dataGridView为什么不直接绑定DataTable,而要用BindingSource呢?

zhaigates 2009-05-21 08:30:00
如题...
...全文
5145 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
窗户纸 2011-05-19
  • 打赏
  • 举报
回复
bindingsource.DataSource = dataset;

dataGridView2.DataSource = bindingsource;
需要指定特定的表名 dataGridView2.DataMember = "tbExam";
zhaigates 2009-05-22
  • 打赏
  • 举报
回复
不使用 BindingSource 作为控件和基础数据源的中间层也行,但是 BindingSource 在很多时候非常有价值,这里只展示了一个很小的方面。

1. ListControl.DataSource 属性

获取或设置此 ListControl 的数据源。实现 IList 或 IListSource 接口的对象可以作为数据源,如 DataSet 或 Array。

假设现在有两个对象:List<T> 的 mylist,ListBox 类型的 listBox1。下面这条语句可以使 mylist 的内容显示到 listBox1 中(方法一):

listBox1.DataSource = mylist;

2. BindingSource 类

封装窗体的数据源。BindingSoiurce 实现的接口有:Component, IBindingListView, IBindingList, IList, ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider。

假设现在还有一个 BindingSource 对象 bsrc ,下面的语句也可以使 mylist 的内容显示到 listBox1 中(方法二):

bsrc = new BindingSource(mylist, null);
listBox1.DataSource = bsrc;

3. 为什么需要 BindingSource ?

先看看 BindingSource 除了让控件知道从哪儿读取数据,还能做什么:

它通过提供一个间接寻址层、当前项管理、更改通知和其他服务简化了窗体中控件到数据的绑定。这是通过将 BindingSource 组件附加到数据源然后将窗体中的控件绑定到 BindingSource 组件来实现的。与数据的所有进一步交互,包括定位、排序、筛选和更新,都通过调用 BindingSource 组件实现。
值得注意的最后一句话,我在《对象引用的保护措施》中提出的“包装类”方案就是想让包装类做到这样。

总之,BindingSource 可以监视(直观地说是“监视”,本质上是“代理”)数据源的变化。在上面的方法一中,listBox1 不能跟踪 mylist 的变化,比如由这条语句引起的数据变更:

mylist.RemoveAt(3);
如果要让 listBox1 反应 mylist 的最新情况,需要:

listBox1.DataSource = null;
listBox1.DataSource = mylist;
或其它可以使 listBox1 与 mylist "从头开始"的语句。

From:http://home.aaaad.cn/space.php?uid=56673&do=blog&id=14667
rockywu 2009-05-22
  • 打赏
  • 举报
回复
好帖子,值得收藏.....
hhbkiller 2009-05-21
  • 打赏
  • 举报
回复
说个简单的例子,lz可以直接把一个list<t>绑给datagridview,试着删除一下看看,你会发现报错哦,而如果通过binddatasource多了这个中转后,很多工作它可以帮助你来完成,实现前面的效果就会很简单.
lxlongnw 2009-05-21
  • 打赏
  • 举报
回复
返回一个数据集dataset1丢给datagridview,即
datagridview1.datasource=dataset1.Table[0].Defaultview
iloveppmm 2009-05-21
  • 打赏
  • 举报
回复
到还没仔细考虑过。

不过都是直接把datasource设置为datatable了。
willwayer 2009-05-21
  • 打赏
  • 举报
回复
datagridview可以使用各种数据源,它本身是不知道这些数据来源
Dobzhansky 2009-05-21
  • 打赏
  • 举报
回复
关于数据绑定, 还有一个重要的东西: BindingContext
Dobzhansky 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zhaigates 的回复:]
看到国外的一个类似问题的回答
和9楼的几乎一样
The BindingSource class was introduced because people were used to having the notion of a "current" record combined ^with^ the data set.

You can access the current values of the "row" in the BindingSource through the indexer (the same way you would with a DataRow).

If you need to cycle through the rows, you can use the MoveFirst,MoveNext, Move…
[/Quote]

嘿嘿
jstzcl007 2009-05-21
  • 打赏
  • 举报
回复
我也一直有这个疑问,用BindingSource后自动生成相应的TableAdapter,筛选、排序、保存数据是方便,但是好像不好用事务,这是一个遗憾,或者我没找到方法,不知大家是怎样做的?
zhaigates 2009-05-21
  • 打赏
  • 举报
回复
看到国外的一个类似问题的回答
和9楼的几乎一样
The BindingSource class was introduced because people were used to having the notion of a "current" record combined ^with^ the data set.

You can access the current values of the "row" in the BindingSource through the indexer (the same way you would with a DataRow).

If you need to cycle through the rows, you can use the MoveFirst,MoveNext, MoveLast, MovePrevious methods to navigate through the data.

If the possibility of the type of the underlying data source might change, from a design standpoint, it would be better to work with the BindingSource, since that is abstracting the underlying data type.

However, if the underlying data source type is "baked", meaning it will not change, then work with the data set.

I will typically construct my own DataView when working with a DataTable and I need to perform specific sorting or filtering. If I don't have a need for that, then I access the DataSet directly.
zhaigates 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Dobzhansky 的回复:]
DataTable 做数据绑定时, 真正用的是 DataView,

下面就来比较一下 DataView 和 BindingSource:

C# codepublicclassDataView : MarshalByValueComponent, IBindingListView, IBindingList, IList,
ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitializepublicclassBindingSource : Component, IBindingListView, IBindingList, IList,
ICollection, IEnumerable, ITypedLis…
[/Quote]
很详细 学习
Dobzhansky 2009-05-21
  • 打赏
  • 举报
回复
DataTable 做数据绑定时, 真正用的是 DataView,

下面就来比较一下 DataView 和 BindingSource:

public class DataView : MarshalByValueComponent, IBindingListView, IBindingList, IList, 
ICollection, IEnumerable, ITypedList, ISupportInitializeNotification, ISupportInitialize

public class BindingSource : Component, IBindingListView, IBindingList, IList,
ICollection, IEnumerable, ITypedList, ICancelAddNew, ISupportInitializeNotification,
ISupportInitialize, ICurrencyManagerProvider


可以看出, BindingSource 比 DataView 多实现了一个接口 ICurrencyManagerProvider

通过查看 MSDN 了解到, 这涉及到在数据源层次维护一个当前位置的概念.

尽管我们在界面上可以看到当前记录是哪一个, 但是使用 BindingSource 我们可以直接用它来对数据进行导航,
导航的结果也会反映到 UI 上,
说到导航, 引出 BindingNavigator 类, 而 BindingNavigator 需要的数据源正是一个 BindingSource 类型的.

所以:
在涉及到数据源当前位置定位以及导航时, BindingSouce 比 DataView 胜出一局.

(注: 使用 DataView 也会自动生成一个 CurrencyManager)
zhaigates 2009-05-21
  • 打赏
  • 举报
回复
偶是这么用的
感觉也很方便...
ch5201314zt 2009-05-21
  • 打赏
  • 举报
回复
所以我觉得还是用DataTable方便些,直接绑定了!
ch5201314zt 2009-05-21
  • 打赏
  • 举报
回复 1
[Quote=引用 3 楼 cxcco 的回复:]
如果提交数据的话,那么就需要使用bindingSource,因为datagridview可以使用各种数据源,所以它本身是不知道这些数据来自哪儿的。所以就需要一个媒介。bindingSource是钦定的媒介,它一边连接着DataGridView,另一边连接着DataTable。它可以让DataGridView结束编辑状态,同时可以将最新数据提交到DataTable中。
[/Quote]
不对啊!

用DataTable绑定的话,DataGridView可以直接提交的啊!

每次编辑结束,当前的DataGridView的数据就提交了,也就是说DataTable的数据改变了!
zhaigates 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Dobzhansky 的回复:]
DataTable 因为有 DataView , 使得使用 BindingSource 的优势不明显.

其实楼主的问题是:

使用 BindingSouce 而不是使用 DataTable, 获得了哪些好处?
或者说,
使用 DataTable 而不用 BindingSource, 我们损失了什么功能?


否则, 一切都是扯淡.


这个问题还真没深入考虑过.
[/Quote]
对头
一个DataGridView直接就绑定了一个DataTable,不会更改
不少地方说用BindingSource,但是说的原因有点模棱两可
Dobzhansky 2009-05-21
  • 打赏
  • 举报
回复
DataTable 因为有 DataView , 使得使用 BindingSource 的优势不明显.

其实楼主的问题是:

使用 BindingSouce 而不是使用 DataTable, 获得了哪些好处?
或者说,
使用 DataTable 而不用 BindingSource, 我们损失了什么功能?


否则, 一切都是扯淡.


这个问题还真没深入考虑过.
cxcco1986 2009-05-21
  • 打赏
  • 举报
回复
如果提交数据的话,那么就需要使用bindingSource,因为datagridview可以使用各种数据源,所以它本身是不知道这些数据来自哪儿的。所以就需要一个媒介。bindingSource是钦定的媒介,它一边连接着DataGridView,另一边连接着DataTable。它可以让DataGridView结束编辑状态,同时可以将最新数据提交到DataTable中。
luckylongge 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
BindingSource 管理与数据源交互的详细信息。
BindingSource 组件可表示任何 Windows 窗体数据源,并在选择或修改数据位置时提供很大的灵活性。
参考
[/Quote]
不同意楼上的观点我认为,用DataTable来实现绑定比较好,它可以按照我们程序员的思路来解决问题,也满足三层结构,而BindingSource是把数据访问层
和界面层混在一起!
加载更多回复(1)

111,088

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧