Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I really want to know how can I pass a whole datagridview values to another datagridview in another form? using c# windows application.

i have two form ,

form1:
i have one search option which is have,
one text box and one search button,
when i type something related to the table field in my text box and click search button,

the corresponding data has displayed to another form datagridview, that means form2.

form2:

i have only datagridview.

please my code see below where i did mistake my code and let me know the solution please..

my code is:


form1:

C#
private void button1_Click(object sender, EventArgs e)
{
      try
      {
            if (textBox1.Text == "")
            {
                 MessageBox.Show("Please Enter the Input Key.", "Error Message");
            }
            else
            {
                 DataTable dataTable = new DataTable();
                 con = new SqlConnection(s);
                 con.Open();
                 cmd = new SqlCommand("select Destinations,TimeofDay,DialCode,EffectiveDate,NewOffer from RateSheets where Destinations='" + textBox1.Text + "'", con);
                 SqlDataReader sqlDataReader = cmd.ExecuteReader();
                 dataTable.Load(sqlDataReader);
                 con.Close();
                 form2 fm = new testing();
           
                 fm.Show();
                 dataGridView1.DataSource = dataTable;
             }                   
       }                   
       catch (Exception ex)
       {
             MessageBox.Show(ex.ToString());
       }
}

Thanks in advance...
Posted
v2
Comments
Orcun Iyigun 26-Feb-13 8:43am    
maybe you may want to change your where statement with LIKE '%textbox1.text%' instead of equals. In your current code do you have any exceptions?

1 solution

 
Share this answer
 
Comments
Member 9989542 18-Apr-13 0:54am    
i have two tables sql (database name vijay)
table1 table2

name age sex name marks1 marks2 total
vijay 15 m vijay 50 45 95
ramu 14 m vijay 45 60 105
ram 15 m ram 50 45 95
lux 15 f ramu 50 45 95
ramu 50 55 105
lux 50 45 95

i make these two tables in two gridview in c# windows form
nw when i click the 1st gridview row (name) , i need to filter name which present in second gridview and show that data in secondgridview
i need a stored procedure and need to call that in c#...

ex
when i click name vijay in firstgridview i need like below in secondgridview
name marks1 marks2 total
vijay 50 45 95
vijay 45 60 105
Orcun Iyigun 18-Apr-13 2:36am    
why would you need to do the binding in the stored procedure?? HAve a look at this link[^]. It is gridview's click events. You can use cell click event and on this event you can bind your second gridview to filter your results. It is more easier in my opinion.

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