简体   繁体   中英

Linked DataGridViews Not Updating

I have 2 pairs of Master-Details (dgvs) datagridviews that are linked together. I also use binding sources for each table. See image.

Table Relationships

When I click on a row for the Link_Contract_Letting dgv, it updates the Contracts dgv, but does not update the Deses dgv. If I click on the Contracts dgv the Deses dgv does update. How can I get the Deses dgv to update when someone clicks a row in the Link_Contract_Letting dgv?

This code is close. It shows the correct contract and filters the deses. The problem with it is the other contracts related to the Letting are removed. That is a problem because I cannot see the other related contracts.

string contId = dgvLinks.SelectedCells[0].Value.ToString();
bsContracts.Filter = $"ContId IN ({string.Join(", ", contId)})";

This code does not work at all, but does not show an error:

string contId = dgvLinks.SelectedCells[0].Value.ToString();
bsDeses.Filter = $"ContId = " +  contId;

I have also tried Refresh() and EndEdit() after the above code.

I wanted to provide an update to answer my question. I have 4 tables in my dataset and 4 datagridviews on my form. In addition, I have 4 binding sources. The 4 tables are related as shown in the link above. My question was why, when I select a record in my many to many Linking dgv I get the correct contract in my Contracts dgv, but I do not get any corresponding Deses in the dgv?

I recently started working with binding sources and have been very confused using them except in very typical situations. I have noticed that there are a lot of questions on binding sources, and I have not found enough articles for me to have a good understanding. Somewhere I came across an article that mentioned “Data Binding with Windows Forms 2.0” by Brian Noyes, so I bought the book even though it was old. I have read 5 out of the 10 chapters. I highly recommend it for anyone in a similar situation. The book explains binding sources very, very, well.

What I now understand is that I was programmatically selecting the Contracts dgv record instead of programmatically selecting the record from the binding source.

To populate the Deses from clicking on the Linking dgv requires 3 lines of code that I placed in the dgvLinks_CellClick handler as shown:

//I get the contract id from the Links dgv that is selected. (Current Row)
int contId = (int)dgvLinks.CurrentRow.Cells["ContId"].Value;

//IMPORTANT: I find the row index that has the contract id in the contracts binding source.
int index = bsContracts.Find("ContId", contId);
//Using the row index of the contracts binding source I set the current row in the contracts binding source which updates my contracts dgv.
bsContracts.Position = index;
//Now since the Contracts and Deses are tied together as Master/Detail
//The Deses dgv shows the correct filtered Deses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM