亲爱的所有人, 我在用户控件之间传递值时遇到问题.我创建了两个用户控件:一个用户控件包含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.
更多推荐
在用户控件之间传递值
发布评论