Hiding the gridview columns based on multiple selection values of a listbox

asoundmind

Member
Joined
Apr 14, 2007
Messages
6
Programming Experience
Beginner
Hallo there, I am having a problem on hiding gridview columns based on values selected on a listbox. For example I have a gridview, a listbox and a button. The gridview consists of 3 columns: userID, FirstName and LastName. So Does the listbox contains 3 items: userID, FirstName, and lastname. What I need is, if user clicks on FIRSTNAME in the listbox and click on the button, the FIRSTNAME column can be hidden. I have tried to use gridview.columns(i).visible but this is not what I want 'coz the values of i are determined of the order of listbox. Thus, it makes column with the order of i invisible. How to select the column based on its name (userID, FirstName, LastName)? Is there a better solution to achieve what I need? Appreciate if you guys can help me with this problem. Thank you :D
 
Just check all columns until you find the matching name:
VB.NET:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    For Each dcf As DataControlField In GridView1.Columns
        If dcf.HeaderText = ListBox1.SelectedItem.Text Then
            dcf.Visible = False
            Exit For
        End If
    Next
End Sub
 
Back
Top