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

Initialize Checkboxes based on multi-DataTable conditions

This question picks up on an earlier question at: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21781470.html

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


  • 2
  • 2
1 Solution

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?

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?

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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