Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am passing value to another form but Gridview not showing any data
C#
dtSearched = objSearching.HadithSearching();
//FrmMain is object of my Frist Form
            FrmMain.dgvAllHadithSearched.DataSource = dtSearched;
            dgvSearchHadith.DataSource = dtSearched;
Posted

1 solution

That's a poor idea, even if it did work for you. The problem is that by making your form controls public you "lock" the design of the form, and tie the two forms together so that they can't be changed without considering the effects on the other. It is a much, much better idea to keep the controls private (as they are by default) and only allow access via properties: in this case a public DataTable property in the form the above code is executing in. You then signal an event, which the main form handles, causing it to fetch the data if it needs it.

See here: Transferring information between two forms, Part 2: Child to Parent[^] - the parent is the main form, the child is teh one providing the DataTable.
 
Share this answer
 
Comments
Muhamad Faizan Khan 4-Nov-13 14:29pm    
i didnt make parent child form. But i am accessing form2 from Main Form
OriginalGriff 4-Nov-13 15:36pm    
But somewhere in MainForm you have the equivelant of:
Form2 form2 = new Form2();
form2.Show(); //Or ShowDialog()
For these purposes, MainForm is a Parent, and form2 is a child, because MainForm has to know form2 exists, and form2 doesn't need to know MainForm exists.

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