Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm working on windows application.

I want to fetch data from one datagridview to another datagridview

I have brought data in from database to datagridview1.
I want that when I doubleclick or press enter on particular cell of datagridview1 it should display the values in second datagridview i.e datagridview2.

Note: I have dynamically created the first datagridview i.e datagridview1
and display the value in that from database.

I also want to know how to create the event dynamically.

thanks
Posted
Updated 24-Sep-18 22:12pm
v5
Comments
T.Saravanann 1-Sep-10 3:21am    
what do you want to show in second grid,All column value of selected row or particular column of selected row.
Dalek Dave 1-Sep-10 3:34am    
Edited for Readability, Spelling and Grammar.

I assume this is not a web app, because if it was, you'd have told us. Just remove the item from your datasource and add it to a list, which will be the datasource for the second grid, then rebind both data sources.
 
Share this answer
 
Hi Sagar55,

DataTable dtOutput=new DataTable(); // Assign in Global

In your DataGridView1 CellBeginEdit Event write the following code
//Its Working in Cell Double Click 

void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e)
 {
   DataTable dtGrid1=(DataTable)DataGridView1.DataSource;  

   string sId=DataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
  //Here 'Id' is Filter Column (Assume)
  DataRow[] drFilter=dtGrid1.Select("Id="'+sId+'"");
  DataTable dtData=new DataTable();
  dtData=dtGrid1.Clone();
  foreach(DataRow dr in drFilter)
    { 
     dtData.ImportRow(dr);
    }
   if(dtOutput.Columns.Count==0)
    {
     dtOutput=dtGrid1.Clone();
    } 
   dtOutput.Merge(dtData);
   DataGridView2.DataSource=dtOutput.Copy();
}


I hope this code useful to you.

Cheers :)
 
Share this answer
 
v3
Comments
Dalek Dave 1-Sep-10 9:40am    
Just put a code block in.
BTW, Good Call on that.
while creating datagridview1 assign a event to it like

datagridview1.CellDoubleClick+=new DataGridViewCellEventHandler(datagridview1_CellDoubleClick);


create one method like :
private void datagridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
                string cellValue= dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
              //create datasource base on your cellvalue and assign that datasource to datagridview2

        }
 
Share this answer
 

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