在用户控件之间传递值

编程入门 行业动态 更新时间:2024-10-28 12:16:44
本文介绍了在用户控件之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的所有人, 我在用户控件之间传递值时遇到问题.我创建了两个用户控件:一个用户控件包含treeView,一个包含datagridview.我想这样做:从TreeView中单击一个节点. datagridview将加载按treeView节点过滤的数据. 谢谢您的答复.

Dear all, I have a problem with passing value between usercontrols. I create two usercontrols: one usercontrol inculde treeView and one include datagridview.I would like to do: click a node from TreeView -> datagridview will load data which filter by node of treeView. Thank you for your reply.

推荐答案

使用属性在控件/类之间传递值. Use properties to pass values between controls / classses.

详细介绍Abhinav"答案是,您需要使用保存您的节点值的DataGridView在表单中创建一个Property.在属性的设置器中,您可以调用一个方法来过滤结果并重新绑定DataGridView.像这样的东西: To elaborate on Abhinav''s answer, you need to create a Property in your form with the DataGridView that holds your node value. In the Setter for the property you could call a method that filters your results and rebinds the DataGridView. Something like this : private string nodeValue; public string NodeValue { get { return nodeValue; } set { if (nodeValue != value) { nodeValue = value; NodeValueChanged(value); } } } public frmDataGridView(string nodeValue) { this.NodeValue = nodeValue; } void NodeValueChanged(string newNodeValue) { //get new results with newNodeValue as filter }

然后,在带有TreView的表单中,您需要创建一个引用GridView表单的私有变量,然后每当树形视图选择更改时,在树形视图选择更改事件中,您只需要像这样更改属性即可:

Then in your form with the TreView you need to create a private variable that references your GridView form, then whenever the treeview selection changes, in the treeview selection changed event you just need to change the property like this:

private frmDataGridView gridView; public frmTreeView() { gridView = new frmDataGridView(); gridView.NodeValue = "DefaultValue"; } void TreeViewChanged(object sender, TreeViewChangedEventArgs e) { gridView.NodeValue = "Selected TreeView Value"; }

希望这会有所帮助. 免责声明.尚未检查此代码,我使用的TreeViewChanged事件是虚构的,因此您需要使用正确的TreeView更改事件.

Hope this helps. Disclaimer. This code has not been checked, the TreeViewChanged event I have used is fictitious, so you need to use the proper TreeView changed event.

希望此 [ ^ ]可能会为您提供帮助. Hope this[^] might help you.

更多推荐

在用户控件之间传递值

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

发布评论

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

>www.elefans.com

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