Click here to Skip to main content
14,928,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGridView that could possibly switch its display values from 2 different dataTables, the information within is really simple, its a fixed 20rows x 20cols table.

Each cell must contain an object, So when i select a cell i can display its extended properties like MaxValue, MinValue, TotalValues, and so on (not just the AVG value which is the one displayed in the cell itself)

I can easily do that by storing the object in the TAG property of each cell in the DataGrid, but the problem I am facing is that when I change the datasource of my DataGridView all that information is NOT stored in the underlying dataTable.

What would be the best approach to solve this?

Looking for best recomendations

As a reference, the object to be stored is custom object and can implement or have any number of properties/attributes as needed
I should also clarify that 1 different object is needed for each cell, not 1 object by row

What I have tried:

I believe I could use 2 different datagridView controls and switch between them but that seems dumb, as there is now 2 datatables but there could be more.

I also thought of storing the objects in an external arraylist (rowIndex/colIndex) and then just search for the object when needed, but again i would need N-arrays for each datatable and doesn't sound about right?
Posted
Comments
Maciej Los 22-May-18 16:25pm
   
I'm sorry, but storing objects in every single cell inside datatable object sounds terrible.
Rather than it, you should use BindingSource[^].
For further details, please see: How to: Bind Objects to Windows Forms DataGridView Controls | Microsoft Docs[^]
But i can be wrong...
creizlein 24-May-18 0:02am
   
thanks for the tip! i will check it out
I do still to find a place to store the objects anyways, so the approach of using an arraylist to store the objects and cross-reference them with the x/y cell is about the same as databinding

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