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>

Recommended Answers

All 3 Replies

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>

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.