Solved

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

Posted on 2009-05-14
7
348 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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