Click here to Skip to main content
15,917,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello There,

I have made a C# application.
I want to make use of DataGridView for retrieval of data. But the twist is DataGridView control is related to different form and I want to extract its data in another form.

Kindly Suggest.
Regards
Posted

1 solution

Don't.

Instead provide a method or property in the form containing the DataGridView which supplies the required info - either a Property returning a list or datatable, or a method to get a specific item.
If you allow access to you controls outside the form then you lock the two forms together: you can't change the DataGridView form without considering the effects that might have on external forms. This is very bad practice, and against the principles of OOP. It also makes your job a lot harder later on!
 
Share this answer
 
Comments
Mayank Topiwala 16-May-12 2:38am    
Advice accepted Sir
However I made use of properties to retrieval of data from DGV in Form2. But it can only allow one value to be returned. And I applied for loop to fetch all rows in Form1 and now want to make use of these data for further queries !!
OriginalGriff 16-May-12 2:48am    
It depends on what type of property you declared.
If it is a string, then it returns a string.
If it is a List<string> then it returns as many strings as you want. Similarly, if it is a DataTable it can return rows and columns!
There are other ways, such as adding your own return class, which could implement IEnumerable and be used in a loop directly for example.
But for simplicity, look at returning a collection in your property instead of a single object.
Mayank Topiwala 16-May-12 2:52am    
That means to make use of List ??
OriginalGriff 16-May-12 3:00am    
It's one way to do it - I don't know what your app or data looks like, so I can't be specific!
Mayank Topiwala 16-May-12 2:57am    
What happened if I use DataGridView in Form2 and set its datasource DGV of FORM1 !!

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