Initialize Checkboxes based on multi-DataTable conditions

Posted on 2006-03-21
Last Modified: 2008-02-01
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


Question by:juststeve
    LVL 12

    Accepted 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?


    Author Comment

    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.
    LVL 12

    Expert Comment

    Post a link to it here as well.

    So you don't want it to persist state? Could you just disable the checkboxes?

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now