Initialize Checkboxes based on multi-DataTable conditions

This question picks up on an earlier question at:

I've refined my problem down to a fairly narrow focus. I'm able to accomplish all the dynamic Table/Column/Row generation and only need to solve the issue of correctly initializing the 'checked' or 'not check' property of the table's checkboxes.

Here's a sample of the sort of table i'll need to end up with:

           Module1    Module2   Module3  Modulex
TitleA         .            .             |           .  
TitleB         .            |             |           .  
TitleC         .            .             .            .  
             . = unchecked boxes
             | = checked boxes

As each cell is constructed it establishes titleID (CurrentRow) & moduleID (CurrentColumn). If the FKey table has a record that matches both titleID and moduleID then the checkBox control should initialize to 'checked'.

I've tried a few approaches but I can't figure out how to implement that stratagy.

Dim ds As New DataSet

Dim GetTitles = "select distinct strTitle, idTitle from FacilityTitles where FacilityID = 1 order by strTitle;"
GetTitles = GetTitles & "select ModuleName, moduleID from modules where moduleID > 0 order by ModuleID; "
GetTitles = GetTitles & "select moduleID, idTitle, ReqModID from RequiredModules where dateDue = '" & lbGetDateDue.SelectedValue & "'  order by Moduleid "
ds = SqlHelper.ExecuteDataset(connStr, CommandType.Text, GetTitles)

ds.Tables(0).TableName = "Titles"
ds.Tables(1).TableName = "Modules"
ds.Tables(2).TableName = "FKey"

'build table
Dim tb As New HtmlTable
tb.CellSpacing = 1
tb.Border = 1
Dim tbr As New HtmlTableRow
Dim tbc As New HtmlTableCell

tbr.Cells.Add(tbc)    ' empty cell in the corner

'build first row as Class 1 - X
For Each myModule As DataRow In ds.Tables("Modules").Rows
    tbc = New HtmlTableCell
    Dim lbl As New Label
    lbl.Text = myModule(0)

tb.Rows.Add(tbr) ' Add the row to table

        Dim intRow As Integer = 0
        For Each myTitle As DataRow In ds.Tables("Titles").Rows
            'Dim myCurModuleID = ds.Tables("Modules").Rows
            Dim myCurTitleID = myTitle(1)
            tbr = New HtmlTableRow
            tbc = New HtmlTableCell
            Dim lbl As New Label
            lbl.Text = myTitle(0)
            tbr.Cells.Add(tbc)   ' Add Title A - Z to first cell of each row

            Dim i As Integer
            For i = 0 To ds.Tables("Modules").Rows.Count - 1
                ' each iteration adds row
                ' with an unchecked checkbox
                tbc = New HtmlTableCell
                Dim cb = New CheckBox

                'iterate thru all records in FKeys table
                'to identify which rows have a idTitle field
                'that matches the current idTitle (myTitle(1))

            tb.Rows.Add(tbr) ' add this row to table
            intRow += 1
            'initialize checkboxes to correct state
            'generate resultset per row


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


I think you have it exactly right when you say:

                'iterate thru all records in FKeys table
                'to identify which rows have a idTitle field
                'that matches the current idTitle (myTitle(1))

Inside this loop you have your current row defined by the outer loop, and the current column defined by the Modules loop. Is that correct? So when you are inside the inner loop, you just have one more loop where you go through all the rows in the FK table until you find one which matches your titleId and moduleId. The psuedo-code will be:

   Dim blnChecked As Boolean = False
   For Each keyMapRow As DataRow In ds.Tables("FKey").Rows
      If keyMapRow(1) = titleId and keyMapRow(2) = moduleId Then
         blnChecked = True
         Exit For
      End If
   cb.Checked = blnChecked

Now, this is slightly inefficient, and you can improve the performance if the ForeignKey table is ordered by titleId and then moduleId. If that is the case, you could maintain an integer variable which is the index of the last row checked, and interate up from there to find your match. If you find it then you set the integer "lastMatch" variable to the new row index, otherwise you leave it as it was.

Does this work?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
juststeveAuthor Commented:
It works but it's persisting state...any changes I make - checking non-checed boxes or unchecking checked boxes - are persisting after a postback. I've verified the code _is running from the top on a postback and I've disabled viewstate...I'm going to spawn a new question to handle this part.
Post a link to it here as well.

So you don't want it to persist state? Could you just disable the checkboxes?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.