Implementing a "Many to Many" relationship with 2 datagrids
- From: Keith <Keith@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 20 Jun 2006 10:07:02 -0700
Simplified Background:
First, I'm kind of new to datagrids, so I hope I can get my question across
right.
I have a database with the following three tables:
PersonTBL: A single record = a person
CourseTBL: A single record = a single college course
PersonHasCourseTBL: Pairs up a Person with a Course - A single record = a
single person/course combination. (Has foreign keys to the other two tables)
One person can have many courses
One course can have many people
What I want to have is two (editable) datagrids:
Datagrid 1: Displays the unique people.
Datagrid 2: Displays all of the courses for the current selected row
(person) in Datagrid 1
It is easy to display the data:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strSQL As String
strSQL = "Select * from PersonTBL"
objAdapterPerson = New SqlDataAdapter(strSQL, objConn)
objAdapterPerson.Fill(objDS1, "Person")
DataGrid1.DataSource = objDS1.Tables("Person")
Dim strPersonID As String
strPersonID = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
objDS2 = New DataSet
strSQL = "Select * from CourseTBL"
objAdapterCourse = New SqlDataAdapter(strSQL, objConn)
objAdapterCourse.Fill(objDS2, "Course")
strSQL = "Select * from PersonHasCourseTBL"
objAdapterRelationCoursePerson = New SqlDataAdapter(strSQL, objConn)
objAdapterRelationCoursePerson.Fill(objDS2, "Relate")
objDS2.Relations.Add("relPersonCourse",
objDS2.Tables("Course").Columns("CourseID"),
objDS2.Tables("Relate").Columns("CourseID"))
objDS2.Tables("Relate").Columns.Add("Course", GetType(String),
"Parent(relPersonCourse).Description")
objView = New DataView(objDS2.Tables("Relate"))
objView.RowFilter = "PersonID='" & strPersonID & "'"
DataGrid2.DataSource = objView
End Sub
Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.Click
Dim strSQL As String
Dim strPersonID As String
objDS2 = New DataSet
strPersonID = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
objView.RowFilter = "PersonID='" & strPersonID & "'"
DataGrid2.DataSource = objView
End Sub
This displays the data how I would expect, but I already have a bunch of
questions:
1. I have a relational constraint that the course in the PersonHasCourseTBL
table must exist in order to exist in the CourseTBL table. The code I have
right now won't let me add new course information, only type in an ID for an
existing course. I want to be able to add a new course this way.
2. It doesn't seem to "save" these new rows to the dataset when I navigate
through different people. How do I get this to happen?
3. Some of the columns in my course grid I want to hide - Can I simply
manipulate the DataView for this without screwing up the DataSet?
4. Some of the columns in my course grid are lookups (for example - which
building is the course in) - obviously those currently show up as the IDs
into the lookup tables. Is there an easy way to make those columns into some
sort of bound combobox (and allow any changes to flow through correctly)
5. Finally, how do I get any jumbled heap of changes (whether it be adds,
updates, or deletes) back into the database smoothly?
I always want to do things the "right" way (i.e. the way its intended to be
done, with the fewest lines and readable code). I know I can figure out some
ways to do some of these things I mentioned, but slopping things together to
make it work isn't my idea of a good time . Any help, as always, greatly
appreciated.
Oh, and please let me know if you feel any of my current code is bad - I'm
halfway expecting it.
.
- Follow-Ups:
- Re: Implementing a "Many to Many" relationship with 2 datagrids
- From: Stephen Howe
- Re: Implementing a "Many to Many" relationship with 2 datagrids
- Prev by Date: Re: Mirror one table from 1 db to another
- Next by Date: Re: Implementing a "Many to Many" relationship with 2 datagrids
- Previous by thread: Mirror one table from 1 db to another
- Next by thread: Re: Implementing a "Many to Many" relationship with 2 datagrids
- Index(es):
Relevant Pages
|
|