wessamzeidan Posted December 29, 2002 Posted December 29, 2002 Hi, I have a small problem. I'm writing an application to access a database. I have multiple forms in the application. One form has a datagrid that displays data from a dataset. I want to be able to access the datagrid from another form to read a row selected by the user. I did this by changing the constructor of the second form to accept a datagrid, and used this constructor in the first form to pass an instance of the datagrid......I don't know if this is the right thing to do. If there is a better way to this, for example by setting the datagrid to be public, please tell me Thanx in advance WMZ Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
*Gurus* divil Posted December 29, 2002 *Gurus* Posted December 29, 2002 Nope, that is probably the best way of doing what you need to do. Passing a reference in the constructor is a good way of solving the problem of accessing something on one form from another. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wessamzeidan Posted December 29, 2002 Author Posted December 29, 2002 So what I did was right, thanks alot. One more question, can I pass a reference for the whole page if I want to access every thing in it? Thanks again WMZ Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
*Gurus* divil Posted December 30, 2002 *Gurus* Posted December 30, 2002 Yes, you certainly can. Remember to pass it as whatever your form name is, not just Form, if you want to be able to access your custom members on it. Failing that, you could cast it whenever you needed to. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted December 30, 2002 *Experts* Posted December 30, 2002 Remeber that if you pass the form and you want to see the members (the controls, such as the DataGrid), you'll have to change their default protection level to public. It sounds like your second form doesn't really need the grid, just some data from the selected row? If this is true, maybe the first form could extract whatever data is needed from the currently selected row and pass that to the second form. That's usually a simpler method than passing the control or Form reference. Just a thought. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.