Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i get data from datagrid in form2 to datagrid in other form in c#?
Posted

 
Share this answer
 
Take one form form1,take second form i named as FormDestination ,put a datagridcotrol with id as dataGridDest in second form (FormDestination)

Write the below code in from1 button click

private void button1_Click(object sender, EventArgs e)
       {
           FormDestination formDest = new FormDestination();  //FromDestination is my seconcd form

           DataTable tableemp = new DataTable();

           using (SqlConnection con = new SqlConnection("user id=sa;password=just4fun;database=sdp;data source=SITT244"))  //Thease are your sql server credentials
           {
               using (SqlCommand cmd = new SqlCommand("select * from emp",con))
               {
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   da.Fill(tableemp);
               }
           }

           Control[] ctlgrid = formDest.Controls.Find("dataGridDest", true);  //Finding the dataGridDest in form name as FormDestination

           DataGridView grid =(DataGridView) ctlgrid[0];   //Converting the control array to grid control

           grid.DataSource = tableemp;
           formDest.Show(); //Callling the second form from first Form


       }


From which table you bind data to datagrid in first form in the same way assign the same table to datagrid in FormDestination
 
Share this answer
 
v4
Comments
Samsani.v.s.Durga Prasad 10-Oct-12 0:17am    
Give me replay whether you get it or not

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