Click here to Skip to main content
15,613,873 members
Please Sign up or sign in to vote.
1.22/5 (3 votes)
See more:
I want to select a row from data grid view 1 which consist a table from database with values entered and now i want to select a row from data grid view 1 so these selected row consist a name which has a primary key in database now this selected row should display the data in data grid view 2 according to some sql condition that if name column's value of the table in dgv1 is equal to name column's value of the table in dgv2 the data should be displayed and after i select another row in dgv1 new data should be displayed in dgv2 as i select a particular row in dgv1 .

What I have tried:

private void account_list_cell_click(object sender, DataGridViewCellEventArgs e)
            SqlConnection con = new SqlConnection("Data Source=ACCOUNTSNEW-PC1\\SQLEXPRESS;Initial Catalog=account_task2;User ID=sa;Password=dotnet");

            if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                dataGridView1.CurrentRow.Selected = true;

                if (dataGridView1.Rows[e.RowIndex].Cells["name"].Value == dataGridView2.Rows[e.RowIndex].Cells["credit_account"])

                else if (dataGridView1.Rows[e.RowIndex].Cells["name"].Value == dataGridView2.Rows[e.RowIndex].Cells["debit_account"])


Updated 13-Aug-21 1:15am
OriginalGriff 13-Aug-21 6:01am    
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

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 - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Hi ,

Assuming that you have Table A with Fields {int A_ID (primaryKey), Varchar A_NAME }
Table B with Fields {int ID (Primary Key), varchar address, int A_ID (Foren Key)}

After Select queries

1. Filled data in our grid view 1 dgv1

2. and add dgv1 Command field Select

3. on selected index changed event

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
lblAId.Text = GridView1.SelectedRow.Cells[0].Text;

// pass lblAId.text in Sql query  and fill in the dataset 

Gridview2.datasource = youdatset.table[0]

abow code is for web app and for windows code will same only Gridview2.databind(); is now required

the concept is that filling master table a data in Grid1 and on selected index changed event get related data from details Table b and fill in grid2

as I see in your function account_list_cell_click
you can note change a data-bind grid cell value like that you should use data table change their value and update grid view data source

I hope this will use full for you
Share this answer
preetv 13-Aug-21 8:19am    
Thanks a lot sir!

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