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

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

4/14/2007 3:58:52 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 48
Get it on Google Play
Get it on Apple App Store

Hello my friend,

Add the following to your code behind: -

Private Sub HideColumn(ByVal strColumnName As String)

        For i As Integer = 0 To myGridView.Columns.Count - 1
            If myGridView.Columns(i).HeaderText.Equals(strColumnName) Then
                myGridView.Columns(i).Visible = False
            End If
End Sub

Then just use: -


There are 2 advantages of doing it this way.  Firstly, if the column name does not exist in the set, no error will result.  Secondly, because it examines HeaderText to find the column, it will work with any kind of column type, including the asp:TemplateColumn type. 

Kind regards



4/14/2007 7:00:16 PM

Similar Artilces:

Hide Gridview column based on the column value
I have a grid which i bind with values from Database. I have events to edit and update the grid. I am not using SqlDatasource to connect to DB. Rather i am doing the updating of the grid Programmatically. The Grid is populated based on a dropdown selection Gridview has 4 columns, LID(item template column) ,LVal (Item Template and an Edit Item template column), Lookupid (Item Template and an Edit Item template column) and "Edit" link 4 of 10 Dropdown selections have values in Column 3 (lookupid), rest dont. When they dont have values i want to hide that column 3. Of course i can ...

Hide a Gridview column but use the value in that column when the row is selected.
I need to retrieve a column from my database but do not want to display it in the gridview.  However, I need to be able to show that information later in the application.  I am using the wizard control.  I believe I can use the DataKeyValues property of the gridview but can't figure out how to write the code to get the value of the cell in that column when the row is selected so it can be referenced later.  I am using VB.  Thanks for your help!! Found the answer.  Here it is in case anyone else needs it:       &nbs...

Change / hide gridview column value based on value in different colum in same row
Hi allI've come across a unusual problem that I can't work out.  I'm using VB by the way.  I have a gridview that is populated from a "suggestions" database and contains 6 databound columns - ID, Firstname, Surname, Department, Suggestion, HideName. The gridview pulls the details from the database and displays all the values as should be. When a user adds data to the databse they have a option to hide their identity by placing a tick in a checkbox. This all gets updated without any issues. The Firstname and Username get automatically populated from their User ...

Change Another Column Value based on Dropdownlist selected value while gridview onrowediting
Hi everyone. Another gridview question again. I have a gridview, with three columns, Code, Description and Action. The Code column, when I click on the Edit linkbutton on the Action column becomes a dropdown list. How can I do that when I selected a value from the dropdownlist, the value of the description on would be changed to the description corresponding to the dropdownlist? Thanks.   Hi:   From your description I think you can try: 1. Set AutoPostBack=true for your DropDownList. 2. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) &n...

Insert multiple values(multiple columns) based on single dropdown list selection.
What I would like to accomplish:  I have a dropdown list that is an entityref(populated by values from PK in separate table). When user makes selection in dropdown list, then clicks “Insert”, I would like the selected value to be inserted into the applicable FK column, but also would like to have a value equal to the integer selected, multiplied by 50, inserted into another column of the same table. Example with user selecting “2” in the dropdown list, inserting into Table b:table a: Column: Numbers(PK) (1, 2, 4, 8) Table b: Processors(FK) Inserted: “2”Table b: Factor Inserted: ...

how to hide gridview columns based on CheckBoxList selections?
hii'm wanting to hide certain columns based on what the user checks in the checkboxlist (whatever they select, i want to be hidden)i know how to hide columns, but only if i set to hide them eg. GridView1.Columns(0).Visible = False how can i go about hiding columbs based on a checkbox selections?here's what i've come up with so far, but i'm missing something and can't figure it out    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged        Grid...

Pre-selecting multiple ListBox values in Gridview
Language C#, Version ASP.Net 2.0HiI have a GridView which is bound to a table that contains Restaurant information. In the Edit Row there is a Listbox (ListBox1) in the Food Type column, bound to a table containing the food types.<EditItemTemplate><asp:ListBox ID="ListBox1" runat="server" AppendDataBoundItems="True" DataSourceID="FoodTypes"DataTextField="FoodType" DataValueField="FoodType" SelectedValue='<%# Bind("food_type") %>'><asp:ListItem Value="">(None)</asp:ListItem&...

Selecting multiple values in a list ListBox on GridView edit
Hello,  I have a GridView which displays multiple ListBoxes containing database values on edit.  The data being edited in this scenario is a contact which contains ID values linking to other database tables.  For most I am able to bind the current selected value to the list box using the code <EditItemTemplate> &nbsp;<asp:ListBox ID="ListBox1" runat="server" DataSourceID="DataSource" DataTextField="DataText" DataValueField="DataID" SelectedValue='<%# Bind("DataID") %>'></asp...

Selecting multiple value from Listbox control in web page
I have a search web page which has one textbox, listitem, search button and datagrid.1) How can i select multiple value in the listbox control. (Either by using Ctrl or shift key)2) After selecting multiple value from the listbox, how can i use those selected value to find the results in search query.Ex. My query is: select * from table where name = @Name@Name = All selected value from listbox control.Thanks Dim iCnt as Integer = myListBox.Items.Count For i = 0 To iCnt - 1 If myListBox.Items(i).Selected Then'Do something here End If NextHope it helpsZath Set the SelectionMode ...

display sum of GridView Column Values based on checkbox selection
Hi guys, I got stuck again. I have a grid view that displays 3 columns of data from sql table. One of the columns is called capacity and is of type Integer. I have inserted a template field with checkbox control in the gridview which allows users to select different rows of the gridview. I also have some textboxes that I use to collect data and update another sql tabel. One of the textboxes is called number of Items. I need to achieve 2 things. Display the total capacity based on rows selected (useing the checkbox) and insert compare validator that compaires Capacity with numb...

Gridview column and control settings. Disable buttons, checkbox based on row value
I'm using C# Hello, I have a gridview with a checkbox in the first column along with a button in the last column. Based on a rows value (on load), I want to make the checkbox grey or not clickable and then enable the button in the last column for that row. If the value is different I want the checkbox clickable, but I want to hide or disable (make it grey) the button in the last column. I'm using the RowDataBound method and have access to the row as it loads. But do not know how to change the checkbox to grey or disabled (same for the button). Does anybody know how to disable the check...

GridView: Show/hide columns based on column name
I would like to show/hide columns of my GridView based on user events. When the user clicks on a specific button, I would like to show/hide certain columns based on the column's name.  I can't seem to get this working.  When I do GridViewName.Columns, it only allows me to use an index and not a string to find the column.I can easily show/hide the columns based on the index of the column, but I would rather not hardcode index values in my application.  If I were to add or remove a column at a later date, I would have to go back through the code to adjust the indexes. Any sugg...

Default selecting listbox values based on DB values...
I have the following code in .aspx file:<asp:listbox selectionmode="Multiple" rows="6" cssclass="FormElement" datasourceid="Unions" id="ctlUnions" tabindex="25" runat="server" datatextfield="short_name" datavaluefield="union_id"></asp:listbox><asp:SqlDataSource id="Unions" datasourcemode="DataSet" enableviewstate="true"            enablecaching="true" cacheduration="Infinite" cacheexpirationpolicy="Absolute"            ConnectionString='<%$ ConnectionStrings:strDataSource %&g...

About Today's (2005-01-07) Article 'Change the style of a column within a GridView control based on its value'
Hi I read the article, sounded interesting although I've done it several times in asp.net (vb), not in C# as this article is. I wonder what the author tries to prove with this article since you cannot run it and see what it does? Since there's some hints that it might be using the Northend database there's no sign of it in the code, thus you can't do anything. Has the author deliberatly left out the datasource so that noone will ever know what the code does? Or is it so that he never thoght of the datasource since that worked in his machine, and he thought if it does work on hi...

Web resources about - Hiding the gridview columns based on multiple selection values of a listbox - asp.net.presentation-controls

Selection - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Multi-state plans and selection problems
... often similar in design. In fact, it may be hard for consumers to tell them apart. The short answer is network and the longer answer is selection. ...

Doc Rivers Comments on If Kobe Bryant Deserves NBA All-Star Game 2016 Selection - Bleacher Report
Kobe Bryant is getting the legend treatment for the All-Star Game in his final NBA season, and that's totally fine with Los Angeles Clippers ...

Running back LeSean McCoy the lone Pro Bowl selection for Bills
LeSean McCoy's first season with the Buffalo Bills will end with a trip to the Pro Bowl. Maybe. The running back was the lone member of the team ...

Return of Yuvraj, Nehra: Selection gamble may just be the tonic India need
There were quite a few surprises thrown by the selectors as they announced the squads for Australia tour, with Yuvraj and Nehra's selection the ...

Jury selection set in fatal 2014 Bourbon Street gunfight - Fox News
A trial is scheduled to begin in New Orleans for the only person arrested following a 2014 gunfight on Bourbon Street that left a bystander dead ...

Jury Selection to Start in Real Estate Agent Killing Trial
Lawyer expects no trouble seating jury for man charged in Arkansas real estate agent's killing

Rashida Jones Sings & Stars In Boss Selection's 'Flip & Rewind' Music Video
... executive producer Jo Miller of Full Frontal . Rashida recently teamed up with her nephew Sunny Levine for his brand new project Boss Selection ...

Today’s selection of articles from Kotaku’s reader-run community: Why Can’t We Have Nice
Today’s selection of articles from Kotaku ’s reader-run community: Why Can’t We Have Nice [Endings] Things? • Game of the Week - Press X to ...

AP PHOTOS: A selection of pictures from the past week
Albuquerque’s source for local news and live streaming video online. KRQE reports on news, weather, traffic, investigations and sports for New ...

Resources last updated: 1/12/2016 10:07:29 AM