Solved

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

Posted on 2009-05-14
7
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

623 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