Solved

Need to Hide Column in GridView but hidden column can be accessible from code-behind.

Posted on 2009-05-14
7
346 Views
Last Modified: 2012-05-07
I need to hide the Student.No Column in GridView where select statement extracts from DB.

string strQry = "Select Student.No,Student.Name,Student.Address, Course.Name from Student left join Course ON Course.No = Student.No where Student.No=1021";
 
SqlDataAdapter da1 = new SqlDataAdapter(strQry, getcon());
 
da1.Fill(ds, "Stud_Tab");
gvStud.DataSource = ds.Tables["Stud_Tab"];
gvStud.DataBind();

Open in new window

0
Comment
Question by:mahdestiny
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24384034
Assuming you are not generating columns automatically (AutoGenerateColumns = False), in the design mode, set the Visible property of the column in the Fields window which can be opened using the Smart icon on the top-right corner of the grid.
Now it depends, where you need to access the StudentNo column in your code behind. If you wish to decide something during the binding of the grid with the database, use the RowDataBound event of the GridView. Use the sample code provided below to access the StudentNo column (the code assumes that this column is the first column, change value 0 to other index based upon column position).
The other way is to not create a column for it but have it in your SQL querey only and set the DataKeyNames property of the GridView to StudentNo. Now to access the values of this for a certain row, use the second option code.

'Option 1
If e.Row.RowType = DataControlRowType.DataRow Then
 
  Dim intStudentId As Integer = e.Row.Cells(0).Text
 
End If
 
'Option 2
If e.Row.RowType = DataControlRowType.DataRow Then
 
  Dim intStudentId As Integer = GridView1.DataKeys(e.Row.RowIndex).Value
 
End If

Open in new window

0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24391462
Hi,

To hide a column but still make it accessible you can you a css class to set the display:none.

E.g.
In stle section of <head> in aspx page
.HiddenColumn
{
      display:none;
}

And then once you have DataBound the gridview. Student No. seems to be the first column so

gvStud.Columns(0).HeaderStyle.CssClass = HiddenColumn; //this will hie the header of the column
gvStud.Columns(0).ItemStyle.CssClass = HiddenColumn; //this will hide all of the data
gvStud.Columns(0).FooterStyle.CssClass = HiddenColumn; //this will hide the column footer if needed.

Then you can access the data like this : gvStud.Rows(<index>).Cells(0).Text();

Hope this helps
-M3mph15
0
 

Author Comment

by:mahdestiny
ID: 24393533
Hi...
Im not using AutoGenerateColumns = False coz im having checkbox for selecting the gridview row.
<Columns>
                       <asp:TemplateField HeaderText="Select">
                           <EditItemTemplate>
                               <asp:CheckBox ID="checkBoxSelect" runat="server" ONCLICK="jsFunctCheck('Panel2')"/>
                           </EditItemTemplate>
                           <ItemStyle HorizontalAlign="Center" />
                           <ItemTemplate>
                               <asp:CheckBox ID="checkBoxSelect" runat="server" ONCLICK="jsFunctCheck('Panel2')"/>
                           </ItemTemplate>
                       </asp:TemplateField>
</Columns>
Also i need to Hide the columns in Gridview. using code-behind.
---------------------------------------------------------------------------------------------------------------------------
And I have tried this in code-behind:
gvStud.Columns(0).HeaderStyle.CssClass = HiddenColumn;
but throws and Error : " The name 'HiddenColumn' does not exist in the current context."
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24393836
Try using the following code. This was provided at (check second last post): http://forums.asp.net/p/1372682/2875053.aspx

protected void GridView1_PreRender(object sender, EventArgs e)
{
  Table tab = GridView1.Controls[0] As Table;
 
  for (int i = 0; i < tab.Rows.Count; i++)
  {
		tab.Rows[i].Cells[1].Visible = false; //assuming that column you wish to hide is at position 2
  }
}

Open in new window

0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 20 total points
ID: 24393852
An alternate might be the following code:
Source: http://www.c-sharpcorner.com/blogs/BlogDetail.aspx?BlogId=340

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//  hide a cell in the column to hide column 1
  e.Row.Cells[1].Visible = false;
}

Open in new window

0
 

Author Comment

by:mahdestiny
ID: 24401400
Hi,
Thanks both the previous solution works....
0
 

Author Closing Comment

by:mahdestiny
ID: 31581381
Thanks........
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question