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

Posted on 2009-04-07
Last Modified: 2013-11-26
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.
Question by:craigewens
  • 5
  • 3

Author Comment

ID: 24098286
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 :-)
LVL 96

Expert Comment

by:Bob Learned
ID: 24100229
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.

Author Comment

ID: 24105149
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.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


Author Comment

ID: 24105602
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.

Author Comment

ID: 24106635
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?
LVL 96

Expert Comment

by:Bob Learned
ID: 24106840
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.


Accepted Solution

craigewens earned 0 total points
ID: 24107669
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.
LVL 96

Expert Comment

by:Bob Learned
ID: 24111485
If you are satisfied with your solution, then go with it...

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question