如何将lisbox选定的项目传递到另一种形式 [英] how to pass the lisbox selected item into another form
问题描述
我在form1中使用datagridview....而form2具有列表框.....如果我从列表框中选择特定项目,则要在form1 datagridview中显示的选择的项目
请帮助我....
I am using datagridview in form1....And form2 have listbox.....if i select the particular item from listbox,the selcted item to be displayed in form1 datagridview
pls hlp me....
推荐答案
将form2中的字符串声明为静态.
将选定的列表框值存储在静态字符串中,以便您可以在Form1中访问它.
如有需要,可以随时提供更多输入.
Declare a string in the form2 as static.
store the selected listbox value in the static string so that u can access the same in Form1.
feel free to Provide some more input incase if you need more.
您可以在自定义事件处理程序类的帮助下实现此目的:-
在Form2中:-
1).定义一个customeventhandler事件,例如:-
You can achieve this with the help of custom event handler class:-
In Form2:-
1). Define a customeventhandler event like:-
public static event System.EventHandler CustomerEventHandler;
2).将列表框中的选定项目存储到静态变量中,如:-
2). Store the selected item from the listbox into a static variable like :-
Public static string strListItem=string.Empty();
strListItem=lstBox.selectedValue.ToString()
3).然后在ListBox的SelectedValueChanged事件上:-
3).Then on the SelectedValueChanged event of the ListBox:-
if (CustomerEventHandler != null)
{
CustomerEventHandler(sender, e);
//firing if any change made
}
在Form1中:-
1).在页面加载中添加is代码:-
In Form1:-
1).add the is code on the page load:-
//During this operation, bind the Form1 event with a method
//which will fire from Form2
frmCustomer c = new frmCustomer();
c.CustomerEventHandler += new EventHandler(ShowListItemInGrid);
//This is the method which will fire upon any change
2).以这种方式定义绑定到自定义事件处理程序的方法:-
2). Define the method bind to custom event handler in this way:-
private void ShowListItemInGrid(object sender,eventargs e)
{
....
...
//your code here for searching the selected item from the listbox
}
如果您在此代码中遇到问题,请参考此链接:-
http://www.codeproject.com/Articles/34454/从一个表单传递数据到另一个表单 [创建高级C#自定义事件 [
Please refer this link if you face some problem in this code:-
http://www.codeproject.com/Articles/34454/Pass-Data-from-One-Form-to-Another-Form[^]
Creating advanced C# custom events[^]
Please don''t forget to mark this as your answer if it helps you out.
Thanks
您好,请使用查询字符串发送选择的值,或者将值保存在Session或Cache中.重定向到另一种形式,并从Session或Cache或QueryString访问值.并基于gridview中的显示数据.
Hi, send the selected value using query string or save value in Session or in Cache. redirect to another form and access value from Session or Cache or QueryString. and based on that show data in gridview.
这篇关于如何将lisbox选定的项目传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!