Trying to get datagridview to update from another form

B

BD

I have an MDI application with a datagridview on a childform1 that
will open childform2 with a double-click event on childform1.
Childform2 works on a record and upon closing I want the datagridview
on childform1 to update to reflect the changes. Currently, I close
childform1 forcing the user to reopen it to reflect the changes. I
have coded a method on childform1 to reload the table adapter but have
been unable to access it from childform2. The following code below is
a brief coding:

childform1:
public void DGVReload()
{
this.childform1tableadapter.fill(this.dataset.childform1);
}


childform2:
private void btnSave_Click(object sender, EventArgs e)
{
childform1.DGVReload();
}

Program will not compile and returns the following error code:
An object reference is required for the nonstatic field, method, or
property

I know there is a simple solution, but I honestly have not been
successful. Any help or direction is appreciated.

BDW
 
A

Ashutosh Bhawasinka

You can do something like this in the form1

private void laneDataGridView_CellContentDoubleClick(object sender,
DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataRowView view =
laneDataGridView.Rows[e.RowIndex].DataBoundItem as DataRowView;
if (null != view)
{
DataRow row = view.Row;
BeginLaneEditing(row);
}
}
}

private void BeginLaneEditing(DataRow row)
{
if (null != row)
{
AddEditLaneForm frm = new
AddEditLaneForm(this.laneTypeTable, this.dummyDockListTable, false);
frm.LaneID = row["LaneID"].ToString();
frm.LaneName = row["LaneName"].ToString();
frm.LaneType = row["LaneType"].ToString();
frm.ShippingDock1 = (int)row["ShippingDock1"];

if (frm.ShowDialog() == DialogResult.OK)
{
row["LaneID"] = frm.LaneID;
row["LaneName"] = frm.LaneName;
row["LaneType"] = frm.LaneType;
row["ShippingDock1"] = frm.ShippingDock1;
}
}
}
 
B

BD

You can do something like this in the form1

private void laneDataGridView_CellContentDoubleClick(object sender,
DataGridViewCellEventArgs e)
        {
                if (e.RowIndex >= 0)
                {
                    DataRowView view =
laneDataGridView.Rows[e.RowIndex].DataBoundItem as DataRowView;
                    if (null != view)
                    {
                        DataRow row = view.Row;
                        BeginLaneEditing(row);
                    }
                }
        }

        private void BeginLaneEditing(DataRow row)
        {
            if (null != row)
            {
                AddEditLaneForm frm = new
AddEditLaneForm(this.laneTypeTable, this.dummyDockListTable, false);
                frm.LaneID = row["LaneID"].ToString();
                frm.LaneName = row["LaneName"].ToString();
                frm.LaneType = row["LaneType"].ToString();
                frm.ShippingDock1 = (int)row["ShippingDock1"];

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    row["LaneID"] = frm.LaneID;
                    row["LaneName"] = frm.LaneName;
                    row["LaneType"] = frm.LaneType;
                    row["ShippingDock1"] = frm.ShippingDock1;
                }
            }
        }

Sorry, my bad. I didn't explain why I was doing it this way. The
datagridview represents say open orders. The childform2 that it
opened is another form based on several tables tied by orderID
number. I am not directly editing the datagridview, that is why I
coded it the way I did. I am sure there is a better way, this way
seemed simpler to me. If I am not explaining correctly, please
advise.
BDW
 
A

Ashutosh Bhawasinka

I am not sure exactly what you meant, but let me try...you want to force
a update on form1 from form2.

If thats the case, create an event, say DataUpdated on Form2. Before
opening(showing) the form2, register for this event DataUpdated on form1.

now from form 2, whenever you want to force an update on form1, just
raise this event.

Actually this is the standard way for notifying the parent of anything
(changes).

Hope this make sense.

Thanks & Regards,
Ashutosh
 
B

BD

I think you hit my problem right on the nose. I am trying to update
form1 from form2 on closing form2. I was doing it in reverse. I was
setting up my update event on form1 and trying to call it from
form2.

Thank you very much for you help and assistance. Sometimes we think
too hard about a problem and totally miss the solution.

Again thanks,

BDW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top