Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Form:1 contains a user control, on the button click event of the user control grid , will show form:2. When I update values in form:2 and close it, the user control grid in form:1 should get refreshed,
My code is...

1.// Main Form (FrmMain.cs)
C#
public void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string menu = "";
            if (e.Link.Caption.ToString().ToUpper() != "")
            {
                menu = e.Link.Caption.ToString().ToUpper();
            }
            Show(menu);
        }


C#
private void Show(string mnu)
{
 SamplesList sample = new SamplesList();
  this.maincontainer.Controls.Clear();
  maincontainer.Controls.Add(sample );
}


C#
public void PerformRefresh()
{
SamplesList sample = new SamplesList();
this.maincontainer.Controls.Clear(); 
maincontainer.Controls.Add(sample );                        

}


2.//User Control(SampleList.cs)
C#
public SamplesList()
       {
           InitializeComponent();
           refreshSamplesData();
   }

public void refreshSamplesData()
       {

           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           ds = logic.getFormData("GetSamplesData");
           dt = ds.Tables[0];
           BindingSource bsBindingSource = new BindingSource();
           bsBindingSource.DataSource = dt;
           navGridCompany.BindingSource = bsBindingSource;
           gridControl1.DataSource = bsBindingSource;

       }

private void btnNew_Click(object sender, EventArgs e)
       {
               Form2 frm = new Form2();
               frm.Show();

       }


3.// Form2(Form2..cs)
C#
string strmode;
private void btnClose_Click(object sender, EventArgs e)
        {
                if (strmode == "strnew")
                {
                    frmMain frm = new frmMain();
                    frm.PerformRefresh();
                   
                }
	}
Posted
Updated 24-Sep-14 22:21pm
v8
Comments
george4986 25-Sep-14 0:10am    
post ur code here
MuhammadUSman1 25-Sep-14 0:11am    
Post your code what You are using/doing.
Raul Iloc 26-Sep-14 0:43am    
So did you solve your problem by using the indications from my solution?

You have to use the current object of your frmForm class and not to create a new one, so in order to communicate between two forms objects you should do the next steps:

1.Your main form fromMain class have to implement an interface like the next one:
C#
public interface IMyForm
{
  void PerformRefresh();
}

class frmMain : Form, IMyForm
{
//...
}

2.Then in the Form2 class add a property of type IMyForm and use it in Close event:
C#
class Form2 : Form
{
 public IMyForm MainForm{get;set;}
//...

private void btnClose_Click(object sender, EventArgs e)
{
 if (strmode == "strnew")
 {
  this.MainForm.PerformRefresh();
 }
}
//...
}

3.Finally in your main form when the Form2 object is created you must set its new property like below:
C#
private void btnNew_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MainForm = this;  //This is line is new!
frm.Show();

}
 
Share this answer
 
Comments
ttds 25-Sep-14 2:28am    
Actually I have there form - MainForm.cs, SampleList.cs (user control) and Form2.cs.

1. call 'SampleList.cs' file to show the data list on 'MainForm'
2.then call 'Form2.cs' file from new button of 'SampleList.cs' file
3.After that add new data from 'Form2' then close and want to refresh the data list on 'MainForm'
so, I couldn't try by above code, there have still error!!!
george4986 25-Sep-14 2:35am    
plz check the solution posted below
Raul Iloc 25-Sep-14 3:48am    
Then you should add a property of type IMyForm also in SampleList.cs and use it in similar way like I explained in my solution above. The idea is to create a communication channel between the main form object and the Form2 object.
add these global variable and property to form1

C#
string strmode = string.Empty;

      public string Strmode
      {
          get { return strmode; }
          set { strmode = value; }
      }


remove code u posted in ur question above in close click

C#
private void btnClose_Click(object sender, EventArgs e)
        {
            ///from ur previous code " if (strmode == "strnew")"  i am assuming strmode has value "strnew" .no extra code needed here
        }


add these global variable and property to form2

C#
string strmode = string.Empty;

       public string Strmode
       {
           get { return strmode; }
           set { strmode = value; }
       }


replace btnNew click code to this

C#
private void btnNew_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Show();
            strmode = frm.Strmode;
            if (strmode == "strnew")
            {
                PerformRefresh();
            }

        }
 
Share this answer
 
Comments
george4986 25-Sep-14 2:37am    
replace
string strmode;
of form 2 with below
string strmode = string.Empty;

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