Solved

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

Posted on 2009-05-14
7
341 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now