Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a form form1 and there is a search button for searching. when i click search button then a new form opens i.e. form2 and in form2 there is a data gridview control. i want when i click on datagrid records cell then this selected values comes onto the form1 textbox1 and textbox2.

i used below code in form 2 but textbox not taking.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{


DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();

}
Posted
Comments
[no name] 25-Mar-14 8:46am    
You need a reference to form1 somewhere in form2.

Open form2 as dialog on search button click and take a var selectedRow in form2 code, assign selected row of datagrid on CellContentClick and set dialog=true, now you can access that row. Code to write on search button click in form1 :
Form2 form2 = new Form2();
if (form2.ShowDialog() == true)
{
       textBox1.Text = form2.selectedRow.Cells[0].Value.ToString();
       textBox2.Text = form2.selectedRow.Cells[1].Value.ToString();

}


Code to write in Form2 on cell content click:

C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedRow=dataGridView1.Rows[e.RowIndex];
            this.DialogResult = true;
            this.Close();
        }
 
Share this answer
 
v3
Comments
manish7664 26-Mar-14 1:41am    
Mr Nirdesh .i am not able to used textbox1 and textbox2 in form 2.
here one error coming with red bottom line -->
Error--> only assignment,call,increment,decrement,and new object expression can be used as a statement
Nirdesh Rathore 26-Mar-14 1:54am    
Are you trying to assign a selected row in code block of form2 ?? if yes then thats wrong i asked you to open form2 as dialog in form1 on search button-click event this code block i suggested write should be in that event.
And var rowSelected should be declared globally in form2 and should be assign on cell content click and there you will set dialogresult to true.
Hi,

You can communicate between form using events.

Add to your project file: SearchResultEventArgs.cs:
C#
public class SearchResultEventArgs : EventArgs
{
    public string ResultText1
    {
        get;
        private set;
    }

    public string ResultText2
    {
        get;
        private set;
    }

    public SearchResultEventArgs(string result1, string result2)
    {
        ResultText1 = result1;
        ResultText2 = result2;
    }
}


Add this code to your Form2:
C#
public event EventHandler<SearchResultEventArgs> SearchResult;
private void OnSearchResult(string result1, string result2)
{
    var handler = SearchResult;
    if (handler != null)
    {
        handler(this, new SearchResultEventArgs(result1, result2));
    }
}


Modify your DataGridView CellContentClick event (on Form2):
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
    OnSearchResult(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString);
}


Now in Form1 add this method:
C#
private void form2_SearchResult(object sender, SearchResultEventArgs e)
{
    textBox1.Text = e.ResultText1;
    textBox2.Text = e.ResultText2;
}



And modify code that you're using to call Form2:
C#
using (var form2 = new Form2())
{
    ...
    form2.SearchResult += new EventHandler<SearchResultEventArgs>(form2_SearchResult);
    if (form2.ShowDialog() == DialogResult.OK)
    {
        // ...
    }  
    ...
}


Now few links:
http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx[^]
http://msdn.microsoft.com/library/system.eventargs%28v=vs.110%29.aspx[^]
Step by Step: Event handling in C#[^]
Generic Event Arguments[^]

Hope it helps you :)
 
Share this answer
 
v3

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