I am attmpting to create a CheckBoxList which will have ListItems corresponding to the names of the of the columns. Then, when a user cchecks a name, that column would be made visible (checked) or invisible (unchecked). All columns should be visible in the beginning.
Teh code below works except when it gets to the item = cbl.Items.Add part and then it breaks. That line must be setup wrong but I can't figure it out after many days of research. The (TryCast(column, GridViewDataColumn)).FieldName.ToString() works exactly as it supposed to - creating a string (probably unnecessary since it's already a string) of the name of each column as it goes through the For Each loop.
Protected Sub cbl_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbl.Init
Dim cbl As CheckBoxList = TryCast(sender, CheckBoxList)
For Each column As GridViewDataColumn In grid.Columns
Dim item As ListItem
Dim itemStr As String
If TypeOf column Is GridViewDataColumn Then
itemStr = (TryCast(column, GridViewDataColumn)).FieldName.ToString()
'Code breaks here. Error msg = Expression does not return a value.
item = cbl.Items.Add(itemStr)
'Same error here:
item = cbl.Items.Add("#")
item.Selected = column.Visible