0

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.

Kind regards,
Penn
<code>
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)
Else
'Same error here:
item = cbl.Items.Add("#")
End If
item.Selected = column.Visible
Next column
End Sub
</code>

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by rohand
0

under If condition, can't you use itemStr = column.FieldName.ToString ?

========================================================================================

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.

Kind regards,
Penn
<code>
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)
Else
'Same error here:
item = cbl.Items.Add("#")
End If
item.Selected = column.Visible
Next column
End Sub
</code>

0

Rohand,

Thank you for taking the time to respond. I can use the phrase you suggested but it doesn't change anything. I still get the error ""Expression does not produce a value."

Penn

0

are you getting error from Else part ? if so then there is something going wrong with your column.

===========================================================================================

Rohand,

Thank you for taking the time to respond. I can use the phrase you suggested but it doesn't change anything. I still get the error ""Expression does not produce a value."

Penn

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.