Link to home
Start Free TrialLog in
Avatar of SandhiyaKrishnamoorthy
SandhiyaKrishnamoorthy

asked on

how to access the Datagrid from other form

Here is the question

I have a  Form1 where  i Display a set of results and when a user clicks on a row it opens an other form  form2 where it calculates a
value and should post it back to the a cell in the datagird in form1.
Now in a gloabl module i declared both the forms

public PayGrid as new form1()
public DrawGrid as new form2()

and in the second form i coded it as
paygrid.ds.tables(0).Rows(RowNum)(5)=CalculetedValue

where ds and rownum are declared as public in Paygrid form(ie Form1)
but i get an error system.nullreferenceexception
:Object reference not set to an instance of an object.
why?
is there any easy way around?
Please help me ASAP


Avatar of Snarf0001
Snarf0001
Flag of Canada image

When do you show Form1?  Is it the startup form?  If not, are you calling PayGrid.Show or Form1.Show()?
Avatar of SandhiyaKrishnamoorthy
SandhiyaKrishnamoorthy

ASKER

Actually form1 is the startupform and i am not closing the form1 or hiding it.when we click on the row of the dtatgrid in form1 it opens the form2 and the user inputs the value and it calculates and sends it back to the form1's datagrid
That is not the way to do it,

if paygrid is your original form
and drawgrid is called from paygrid

then just do

In paygrid

dim drawgrid as new form2
drawgrid.owner = me
drawgrid.show

then in form2 you can just do

ctype(me.owner,form1).ds.tables(0).Rows(RowNum)(5)=CalculatedValue

ofcourse ds should be declared public or friend

so no need for a module
Are you actually setting the value of PayGrid then?  That might be the problem, as the line you have in the global module declares paygrid as a completely separate, new instance of the form.

You should have it as
  public PayGrid as form1()

and then on the form_load for form1, put in
  PayGrid = me

This will just declare PayGrid as an object of type Form1, but currently holding nothing.  When form1 is loaded, PayGrid will be set to that instance of the form.

A warning though, this will product very unreliable results if you ever declare another instance of form1.
And the code above mine would be the proper way to do it so you didn't have to worry about unreliable results with multiple instances.
Check the accepted answer in this thread:

https://www.experts-exchange.com/questions/21126753/MDI-FOrms.html

instead of the dataset you will pass your datagrid.

Public MyDataGrid as DataGrid

in your form.
Thanks Guys,
especially thanks Ronalds ur method works very fine.



ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial