Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagridview on my Main Form.

I want to re-bind the data in the datagridview from the second form. When I am clicking on the back button on the second form, it must re-bind the data on the Main Form.

//Main Form

Name of the Main Form is:frmMain
Datagridview name is: dtgAllIngdetTerminals (Modifiers: Public)

I have written the following code:
//Second Form

//Methid to bind the data
 public void ShowAllTerminalDetails()
        {
            frmMain formMain = new frmMain();
            DataTable dt = new DataTable();
            dt = clsIngadmin.listallingdetails();  //reading a short procedure from a class
            formMain.dtgAllIngdetTerminals.DataSource = dt;
        }


//Back button on my second form that is going back to the main Form
C#
private void btnBack_Click(object sender, EventArgs e)
       {
           ShowAllTerminalDetails(); 
           this.Close(); 

       }


If I re-bind the data from the Main form, it works. All the data are displayed in the datagridview. If I am trying to re-bind the data from the second form, nothing is displayed in the datagridview. What am I doing wrong?
Posted
Updated 5-Mar-12 1:03am
v5
Comments
JacoBosch 1-Mar-12 6:31am    
If I just do it on the Main Form it looks as follows:

//Main Form

public void ShowAllTerminalDetails()
{
DataTable dt = new DataTable();
dt = clsIngadmin.listallingdetails();
dtgAllIngdetTerminals.DataSource = dt;
}

private void tlStripRefresh_Click(object sender, EventArgs e)
{
ShowAllTerminalDetails();
}

call your binding method on page load if u want to see the data when page is load first time and also write the same method on refresh button also
 
Share this answer
 
Comments
JacoBosch 5-Mar-12 6:02am    
The method is written on the Page Load, and on the Refresh button. The Refresh on Form1 is working great, but when I want to "refresh" the data from Form2 but I close Form2 no data is displayed inside the datagridview.
What I did to make it work:

I made a Refresh button on a toolstrip. I bind the datatable in the Refresh button on the onclick event.

C#
private void tlStripRefresh_Click(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
            dt = clsIngadmin.listallingdetails();
            dtgAllIngdetTerminals.DataSource = dt;

       }



Then I called the refresh event(tlStripRefresh) of the refresh button where I want to use it.

tlStripRefresh_Click(tlStripRefresh, EventArgs.Empty);
 
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