Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have same data in two datagridview. I select one datagridview cells its affected in another in c#
Posted
Updated 14-Dec-15 22:55pm
v2
Comments
OriginalGriff 15-Dec-15 5:01am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Don't try to be concise: explain what your problem is in as much detail as you can. The better the detail in your question, the better answer we can give.
At the moment, we have no idea what you are having a problem with!
Use the "Improve question" widget to edit your question and provide better information.
Suvendu Shekhar Giri 15-Dec-15 5:02am    
Didn't get your actual problem. Share more info.

1 solution

Just use the Cell Click event of one of the grid

You can add or make changes according to your output:

Consider that i have Two datagridView with same rows and columns and im dynamically adding rows and columnsto both the grids.

C#
private void btnAdd_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

             string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            dataGridView2.ColumnCount = 3;
            dataGridView2.Columns[0].Name = "Product ID";
            dataGridView2.Columns[1].Name = "Product Name";
            dataGridView2.Columns[2].Name = "Product Price";

            string[] row1 = new string[] { "1", "Product 1", "1000" };
            dataGridView2.Rows.Add(row);
            row1 = new string[] { "2", "Product 2", "2000" };
            dataGridView2.Rows.Add(row);
            row1 = new string[] { "3", "Product 3", "3000" };
            dataGridView2.Rows.Add(row);
            row1 = new string[] { "4", "Product 4", "4000" };
            dataGridView2.Rows.Add(row1);
        } 


Secondly I have the CellClick Event of My first DataGrid :

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        { 
            int rowindex = dataGridView1.Rows[e.RowIndex].Index;
            int columnindex = dataGridView1.Columns[e.ColumnIndex].Index;

            dataGridView2.Rows[rowindex].Cells[columnindex].Selected = true;
            
        } 


So when i click one content of my first grid the same cell gets selected in the second grid .

You can add your code in CellClick Event of dataGrid .
 
Share this answer
 
v2

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