0
votes

Let me quickly describe the scenario: 1)In Form1 I have a dataGridView called dgv_Employee and a searchButton. 2)In Form1's pageload event there is a code which fills the dataGridView . Here,I am using linq to sql to fill the data.(eg. dgv_Employee.dataSource = Dbmlobject.Employee;) 3)Whenever a searchButton is pressed a new Form Form2 opens up and a user search for the particular employee by giving proper inputs. 4)When a search button is clicked I want the search result to be displayed in a dataGridView and Form2 to be closed.I have succeeded to close the form2 .however unable to refresh the datagridview in form1 from form2.

Please help me out as I am trying for 3 days .

4
Put your question in proper readable formate. Like use <br> tag in your question to break line and all that. - Priyank

4 Answers

1
votes

You need to use Event and delegate to get your desire output.

Let me give you example.

Suppose There is Two form Form1 and Form2.

  • Form1 contains a DataGridView and search button.
  • Form2 contains a textbox and button named ok.
  • Assuming that DataGridView is filled with employee name and it need to search by name.
  • Form2 is opened when search button is pressed and then after filling employee name on form2 , when ok buttton is pressed Form2 is closed and data is filtered as employee name was filled.

    So to get result as above you can do something like below:

Code for Form1:

private void btnSearch1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
}

what is above code doing need not to explain. <=:)
Now,
Code for Form2:

 public delegate void OnSearch(string employeeName);

public partial class Form2 : Form
{
    public event OnSearch OnSearchClick;
    protected virtual void FireEvent(string employeeName)
    {
        if (OnSearchClick != null)
        {
            OnSearchClick(employeeName);
        }
    }

    public Form2()
    {
        InitializeComponent();
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        string employeeName = txtEmp.Text;
        FireEvent(employeeName);

        this.Close();
    }
}

As, I told you need use event and delegate, here all are in form2.

In Form2 there is a delegate named OnSearch that contains string parameters. and also there is a event OnSearchClick for delegate OnSearch. Here delegate delivered at class level.

In next, FireEvent funcion is created which triggers the event OnSearchClick when this method is called.
FireEvent is called when btnSearch button click. Here employeeName is passed in method you can pass more parameter or object in method and you can get that on Form1 and you can apply filter.

Now time to edit code for Form1:

    private void btnSearch1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.OnSearchClick += new OnSearch(frm2_OnSearchClick);
        frm2.Show();
    }

    void frm2_OnSearchClick(string employeeName)
    {
        MessageBox.Show(employeeName);
    }

Edit code for From1 as shown above, Now when you write frm2. intellisense give you event named OnSearchClick select that and then after one space type += and then press tab. It will generate frm2_OnSearchClick event as you can show above code.

Pass all the parameter from Form2 to the Form1(I have passed only one) and write your logic to bind DataGridView in frm2_OnSearchClick event.

Now, Whenever you click on search button(which is on Form2) it will fire frm2_OnSearchClick event(which is in Form1) and DataGridView will refreshed.

1
votes
 Form2 form2 = new Form2();

 //add handler to catch when child form is closed    
 form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
 form2.ShowDialog();


private void form2_FormClosed(object sender, FormClosedEventArgs e)           
{              
 //when child form is closed, this code is executed   
// Bind the Grid view       
 PopulateControls();                                  
}
0
votes

Add your binding to grid on a method so i can be easilly called anywhere on your events.

private void BindTogrid()
{
 //call again you binding
dgv_Employee.dataSource = Dbmlobject.Employee; 
}

Then Under your

searchButton click()
{
Form2 form2 = new Form2();
form2.ShowDialog(); //Show Form2
 BindTogrid(); //Execute if Dialog has been closed;
}

Hope this help.

Regards

0
votes

Whenever need to refresh grid just reassign dataSource.