Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 777
  • Last Modified:

Populate DataGridView from 3 DataBound DataTables with relational links making dynamic matrix

Hi Experts,

I am using Visual Studio 2005, C#, .Net v2.0, DataSet controls to create XSD which write down to XML files for a Windows Form project. The XSD and XML files are all generated though Visual Studio... no database backends used.

I shall attempt to remove my real-world project names and make more general for ease of understanding. I am also going to simplify the table sizes.

I have three datatables of interest for this question and shall describe my desired outcome first.
I wish to have a DataGridView in a windows form.
The rows shall be peoples names, the columns shall be weekdays.
I wish the user of the DataGridView to be able to apply ticks to the matrix saying person1 did something on monday, wednesday etc and person2 did something on monday, tuesday and sunday etc.
The persons name and quantity of people shall be within Table1.
The days and quantity of days shall be within Table2 (maybe days is a bad example but it's a list which can grow and shrink)
There shall be a Table3 which contains the 'ticks'.

Table1 Columns: NameID[autonumber], Name[string], TicksID[int][unique]
Table2 Columns: DaysID[autonumber], Day[string]
Table3 Columns: TicksID[int],DaysID[int]

I have reservations as to if this is the correct way to implement my task... I welcome alternatives.
I also have no idea how I dynamically populate the DataGridView and bind the data.

I cannot be the first to come up with this kind of matrix requirement so perhaps there is more than one way to skin a cat?

Code snippets would be most welcome and I'd love some help with this problem.
I hope I have explained my situation sufficiently; if not please request additional information.

Thank you for your time.
  • 5
  • 3
1 Solution
craigewensAuthor Commented:
Since I received no replies yet I decided to soldier on and see where it takes me...

I have Table1 and Table2 tables created and populated so decided to create a new form.
Within this form I added a new DataGridView control to it but did not bind to any data sources.
As the form loads, I loop through each row within Table2 extracting in turn the 'Days' and create columns within the DataGridView with a column HeaderText of the 'Day' string.
I then loop though each row within Table1 extracting in turn the 'Names' and add a new row to the DataGridView. I then title the new row with the 'Name' string using HeaderCell.Value.
# | MON | TUES | WED
BOB | X | [] | []
JAY | [] | [] | X
IAN | [] | X | X

On the face of it this seems to work but I have no idea how I store the resulting matrix back into a DataSet defined DataTable which is bound to the original header supplying rows (Table1 and Table2)

I'd ideally like to have Table1, Table2 and a Table3 all definable within the Visual Studio 2005 IDE DataSet designer window and the DataGridView bound to a DataTable.

Please can someone offer the missing piece of the jigsaw?

Perhaps I underestimated this task and as such didn't place enough points up for grabs which is why I've received no help yet... I'm happy to increase if the solution to my issue turns out to be a large one :-)
Bob LearnedCommented:
I think that a better approach would be to use the DataGridView in the virtual mode, where you can control what data is shown, and how that data is shown.
craigewensAuthor Commented:
Thank you for responding...
I have done a little reading on VirtualMode and fail to see what the advantage is over me doing it manually as I am currently having to do.

I create the columns and rows manually by looping through Table1 and Table2 rows and turning their values into column headers and row headers. I store the primary keys for the rows&columns within the Tag property of the header cells.
When the use presses "save" I iterate through the dgv finding a 'tick', where I do, I store the col header.tag in Table3 col1[dayid] and row header.tag in Table3 col2[nameid].
To populate I reverse... I am using the relationships defined within the DataSet designer to ensure (aka throw errors) when I put an id back into Table3 which doesn't exists in Table1 or Table2.

So; that's what I'm doing atm; if you still think VirtualMode is the way to go, please expand if you can; i'd appreciate it.
Thank you for your time.
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

craigewensAuthor Commented:
From reading more into the VirtualMode functionality, it's designed to deal with vast amounts of data and only really loading/presenting data as/when it's required rather than fully populating a control with too much data for it to handle.
For my application, I'm not dealing with a great amount of data, I'm just trying to make a matrix from two tables and have a 3rd (matrix) table which has bound rows/columns to other tables.
craigewensAuthor Commented:
Seems I'm not going to get the desired answer that I wished for and my own way now seems more then stable for the job.

Anyone know how do I close the questions and refund the points?
Bob LearnedCommented:
It's funny how people think that this is my full time job.  I don't sit by my computer 24 hours a day, you know.  *BIG GRIN*

If you are happy with your solution, and you feel that it provides the level of functionality that you need, then my work is done.  If you are looking for a "better" way, then we can continue to hash over ideas.

If you are talking about something like a cross-tab display, then most of the work will need to be done from the backend, since the DataGridView can only bind to a single source.  

The virtual mode is handy if you need to handle complex data requirements, but doesn't fit all situations.

craigewensAuthor Commented:
lol, 'people' realise none of the experts on here work for EE but in some situations people also cannot wait periods of time for an answer which might or might not satisfy their needs. Nor can they solely rely on answers as they also don't work for EE and have their own deadlines.
EE is a resource (noted a great one!) just like any other forum and whilst waiting for responses people also look elsewhere.

My current implementation now works to a satisfactory manner and I guess since I've not had any queries requesting additional clarification I presume there isn't an obvious alternative.
I'm happy to keep the question open if you believe there is a better alternative; but I also don't wish to waste your time.
Bob LearnedCommented:
If you are satisfied with your solution, then go with it...

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now