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

Posted on 2009-04-07
Medium Priority
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.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

601 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