Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hai..

I am using One MDI form and One windows form.MDI form contain one datagridview which contain records from Database(SQL).The Windows form contain Save Button[Adding new data to database].I want to Show the record in datagridview when i click save button.I made modifier of Datagridview as Public,but i can not access datasource property.So please help me sir.


With Thank you
Sujith
Posted

Nasty, nasty, nasty.

Put that public back to private!

When you access form UI elements such as a DatGridView directly from another form, you are locking the two forms together: you cannot change one without considering it's effects on teh other. This is against one of the principles of OOP: Encapsulation.
And a very bad idea!

Instead, create a public property in the form which supplies the datasource in an appropriate format: string, DataTable or whatever. That way, you can change the way your form works without affecting the outside world: you just have to keep the output of your property the same.
 
Share this answer
 
Comments
Nish Nishant 9-Feb-11 17:05pm    
Best answer so far, voted 5, proposed as answer.
Dalek Dave 9-Feb-11 18:00pm    
Good Answer,5!
Hi sujithmtla,

Let MDI form refers to Form1 and Windows form refers to Form2,

first you must create an overloaded constructor in form2 and a global object of form1 , your already existing default constructor is :

public Form2()
{
     InitializeComponent();
}


The global object and the overloaded constructor code will be :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     InitializeComponent();
     Frm1 = form;
}


Now you can set your datagridview in form1 's modifier to public .

Then in the click add button event you will insert this code for opening form2 :

Form2 Frm2 =new Form2(this);
Frm2.show();


I Hope this time it is clear ,
Good luck
:)
 
Share this answer
 
Hi
Try this callback idea. So you won't access anything outside the form1, but let the form1 do its work.
http://www.codeproject.com/Tips/138326/Use-of-delegates-for-form-reusability-Dependency-i.aspx
 
Share this answer
 

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