Home > Back-end >  Change DataGridView Multiple cells value at once when another Grid Cell value Changed
Change DataGridView Multiple cells value at once when another Grid Cell value Changed

Time:10-24

I am having two DataGridView controls in two different tabs of C# Winforms Application, with Visual Studio 2019.

First Grid contains Itemname and Price as following

ITEMNAME        PRICE
---------------------
MOUSE           399.00
PRINTER         12000.00
MONITOR         18500.00

In another grid, I display these items issued/sold to different customers as below

CUSTOMERNAME        ITEMNAME        PRICE
-----------------------------------------
ANANT H             MOUSE           399.00
JOLLY K             MONITOR         18500.00
ANANT H             MOUSE           399.00
KIRAN A             PRINTER         12000.00
KIRAN A             MOUSE           399.00

When price is updated of any item in first grid, I want that to be reflected in second grid against the matching itemname.For example, if MOUSE price is change to 500.00, it should reflect immediately in all 3 cells of PRICE column of second grid, where ITEMNAME matches.

Obvious choice of doing this, is to run a for loop and match with ITEMNAME and get the PRICE cell to edit the value. But this looks heavy and time consuming to me when large number of records are there in second grid.

Is there any idea with which I can change all the value of matching ITEMNAME's PRICE value at once, when it's price changes in first grid?

Like we do in EXCEL where if cells are referenced to a single master cell, when we change master cell price value, all the referenced cell values are automatically changed at once.

CodePudding user response:

Ok, so we can do this with data relations:

  • add a new file of type DataSet to your project (or if you have one already, do it in existing one). Open the dataset file to see a blank design surface (or with existing tables)
  • right click new data table, put a table called Products, with columns of ProductId, ItemName and Price, with sensible datatypes (int, string, float)
  • put a new table called, i don't know, Quotes or something
  • in Quotes put columns of CustomerName, ProductId
  • click in the grey row header of ProductId in Products table so line goes blue, then click drag and drop on ProductId of quotes table - the create data relation window appears, check that Products is the parent table and Quotes is the child table (I always forget which way to drag) - swap in the combo at the top or redo the drag the other way if it's wrong
  • into Quotes table add another column ProductPrice, type float and set its Expression property to Parent.Price
  • switch to a new form designer (for now, just to demo it) and open data sources window (View menu.. Other Windows). Hopefully you're not using net core.. Drag the products and quotes nodes out of data sources and into the form (there are two quotes nodes, one a child of products. Drag the top level ones because you don't need related binding behavior for this demo

Run the app, put some products in, then put a quote in, then change the price. It should update automatically The quote

  • Related