如何将控件绑定到DataGridView(How to bind controls to the DataGridView)

编程入门 行业动态 更新时间:2024-10-26 02:28:19
如何将控件绑定到DataGridView(How to bind controls to the DataGridView)

我是.NET的新手,所以请耐心等待我;)

在我的Windows窗体上,我有一个绑定到数据源的DataGridView。 由于我的网格是只读的,因此网格外部有一组控件(文本框,复选框等),用于编辑数据。

我希望将控件绑定到网格中当前选定的行。 目前,如果我将控件的DataBindings设置为与网格相同的数据源,即使我在网格中移动记录指针,也只会显示第一条记录。

我错过了什么?

环境:Windows窗体,C#,Visual Studio 2008。

I'm new to .NET, so please be patient with me ;)

On my Windows Form, I have a DataGridView that is bound to a data source. Since my grid is read-only, I have a set of controls (textbox, checkbox, etc.) outside the grid that will be used to edit the data.

I want the controls to be binded to the currently selected row in the grid. Currently, if I set the DataBindings of the controls to the same data source as the grid, only the first record is showned even if I move the record pointer in the grid.

What am I missing?

Environment: Windows Form, C#, Visual Studio 2008.

最满意答案

要将其完全保留在Visual Studio的数据绑定环境中,您可以使用两个BindingSource,一个用于DataGridView,另一个用于详细控件。 这与此处的示例非常相似:

http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx

但是,您不是使用详细信息表,而是使用自己的控件来显示详细信息。 这些控件仍可以数据绑定到第二个BindingSource。

或者,只需处理DataGridView上的SelectionChanged事件并编写代码以手动更新控件的值。 第二种方法更轻巧,可能性能稍好一些。

希望这可以帮助!

亚当

to keep this completely within Visual Studio's databinding environment, you can use two BindingSources, one for the DataGridView and another for your detail controls. This is very similar to the example found here:

http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx

But, instead of using a detail table you're using your own controls to show the details. These controls can still be databound to the 2nd BindingSource.

Alternatively, just handle the SelectionChanged event on your DataGridView and write code to manually update the values of your controls. This 2nd approach is a little more lightweight and will probably perform slightly better.

Hope this helps!

Adam

更多推荐

本文发布于:2023-07-17 13:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   如何将   controls   bind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!