Create a large grid with checkboxes?

  • Thread starter Thread starter Klaus Jensen
  • Start date Start date
K

Klaus Jensen

Hi

I am not sure, what is the best way to do this, and I hope somebody can
offer some advice. :)

I have 30 brands (from one table) out of one axis and 30 product categories
(from another table) out another, and I have to register which brands have
what product categories (using checkboxes).

I have created a small image to help illustrate:
http://klausjensen.dk/images/checkgrid.gif

The user then checks some boxes and presses save, and I save the selections
to a third table.

What is the best and easiest way to create such a grid?

Thanks in advance

- Klaus
 
I am looking into pivot, the The Rozenshtein Method and other exciting
stuff now, thanks! :)

Ah, so I take it you're using SQL Server...?

If you need any further assistance with that, you'll probably get the
quickest and best response from microsoft.public.sqlserver.programming - the
guys over there live and breathe this sort of stuff... :-)
 
So Klaus, what did you end up doing? I have a similar situation and
would love to see samples of the ASP and query you came up with.
 
BrentM said:
So Klaus, what did you end up doing? I have a similar situation and
would love to see samples of the ASP and query you came up with.

Hi Brent.

Sorry I did not see your post until now.

I ended up doing the work in the user-interface, no fance SQL. I created a
table (serverside), and then rendered the checkboxes manually. When I save,
I run through all cells in the table and save each cell seperately. It works
very fast, even though I am sure a more serverload-friendly solutiuon could
be deviced.

I have pasted the code below.

Sub RenderGrid()

Dim rw As TableRow
Dim cell As TableCell
Dim chkBox As CheckBox
Dim shopBrandProductCategoriesDataTable As DataTable =
DataAccess.ShopBrandProductCategory.GetForShop(_shopId)
Dim brands As DataTable = GetBrands()
Dim productCategories As DataTable = GetProductCategories()
Dim rowNum As Integer = 0
Dim oddRow As Boolean = True
Dim oddCell As Boolean = True
Dim cssClass As String

'Render Grid
For Each brandRow As DataRow In brands.Rows

If rowNum Mod 5 = 0 Then
rw = New TableRow
rw.Cells.Add(New TableHeaderCell) 'Upper left cell left
blank...
rw.CssClass = "CheckboxGridHeader"

For Each productCategoryRow As DataRow In
productCategories.Rows
cell = New TableHeaderCell
cell.Text = productCategoryRow("ProductCategoryName")
rw.Cells.Add(cell)
Next
tblGrid.Rows.Add(rw)
End If

oddCell = True
If oddRow Then
cssClass = "CheckboxGridItem"
Else
cssClass = "CheckboxGridItemAlt"
End If

rw = New TableRow
rw.ID = "row_" & brandRow("BrandId")

rw.CssClass = cssClass & "2"

cell = New TableCell
cell.Text = brandRow("Brand")
rw.Cells.Add(cell)

For Each productCategoryRow As DataRow In productCategories.Rows
cell = New TableCell
cell.HorizontalAlign = HorizontalAlign.Center
If oddCell Then
cell.CssClass = cssClass & "1"
Else
cell.CssClass = cssClass & "2"
End If
chkBox = New CheckBox
chkBox.ID = "chk-" & brandRow("BrandId").ToString & "-" &
productCategoryRow("ProductCategoryId").ToString
If
ShopBrandProductCategories(shopBrandProductCategoriesDataTable,
brandRow("BrandId"), productCategoryRow("ProductCategoryId")) Then
chkBox.Checked = True

cell.Controls.Add(chkBox)
rw.Cells.Add(cell)
If oddCell Then oddCell = False Else oddCell = True
Next

tblGrid.Rows.Add(rw)

If oddRow Then oddRow = False Else oddRow = True
rowNum += 1
Next

End Sub

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click

Dim chkBox As CheckBox
Dim brandId, productCategoryId As Integer
Dim ids As String()

For Each row As TableRow In tblGrid.Rows
For Each cell As TableCell In row.Cells
For Each ctrl As Control In cell.Controls
If ctrl.ID.StartsWith("chk-") Then
chkBox = CType(ctrl, CheckBox)
ids = chkBox.ID.Split("-")
brandId = CType(ids(1), Integer)
productCategoryId = CType(ids(2), Integer)
If chkBox.Checked Then
DataAccess.ShopBrandProductCategory.Insert(_shopId,
brandId, productCategoryId)
Else
DataAccess.ShopBrandProductCategory.Delete(_shopId,
brandId, productCategoryId)
End If
End If
Next
Next
Next

End Sub
 
Back
Top