Solved

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

Posted on 2009-05-14
7
347 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

827 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