Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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....
Posted
Comments
CRDave1988 22-Feb-12 4:57am    
Can u give more info. like where u have created this two frm? how u r going from one for to other?

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.
 
Share this answer
 
Comments
saravana__ 22-Feb-12 5:04am    
i need some code to access the listbox selected item from another form
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). Store the selected item from the listbox into a static variable like :-
Public static string strListItem=string.Empty();
strListItem=lstBox.selectedValue.ToString()


3).Then on the SelectedValueChanged event of the ListBox:-
if (CustomerEventHandler != null)
    { 
        CustomerEventHandler(sender, e);

	//firing if any change made 
    }


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). 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
}


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
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
fjdiewornncalwe 22-Feb-12 13:09pm    
DataGridView is Windows.Forms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900