Answered by:
Problem with Gridview and non visible columns.

Question
-
Hello,
I have a grid view with 5 columns on a ASP.net page.
In the code behind file im capturing the value of 5th column like this :
dim somevar = GridView1.SelectedRow.Cells(5).Text
The above works perfect.
Now when i make that 5th column's visible property to false i can no longer get a value using :
dim somevar = GridView1.SelectedRow.Cells(5).Text
How should i get reference the column once visible = false
Thank You!
Friday, February 3, 2012 9:26 PM
Answers
-
This is not exact answer to your question but rather workaround on how to get values from hidden fields. For one of your visible cells, you can create template column with asp:Label for visible value. Add hidden control to this column <asp:HiddenField> and bind desired value. The value is preserved on the page and you can get it via SelectedRow.FindControl("NameOfHiddentField") function.
As far as I know, you cannot retrieve text of the cell from the hidden column.
- Proposed as answer by Nitin J Jain Saturday, February 4, 2012 11:16 AM
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 5:12 AM -
Hi,
Its not possible to get value of hidden bound filed from SPGridView. The reason is SPGridView doesn't render hidden bound fields to the client.
You need to add a Template field as mention below:-
<asp:TemplateField> <ItemTemplate> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("BirthDate") %>' /> </ItemTemplate> <EditItemTemplate> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField>
These hidden template fields can be accessible through code.GridViewRow row = GridView.Rows[e.NewEditIndex]; HiddenField hidden = (HiddenField)row.Cells[0].FindControl("HiddenField1"); // lets HiddenField1 is bound to DateTime type field. DateTime birthDate = Convert.ToDateTime(hidden.Value);
- Proposed as answer by Akhilesh Nirapure Saturday, February 4, 2012 7:10 AM
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 5:39 AM -
Of course you can not get the hidden value from GridView you know why?because the hidden column is not exits.
For example in your situation you have GridView with five column then you set visible=false to the last column ,thats mean at rendering time there will be only four columns not five because you hide the five column.So to solve this problem follow the next steps
1- Click in the GridView then go to the GridView events
2- Double click on the RowCreated event then write this code to hide the GridView column
e.Row.Cells(5).Visible=false 'Hide the column
The above code mean "Generate the GridView with five columns then after creating all columns hide one or more columns based on your requirement"Now you can access to the column normally with no need to create template
Dim somevar = GridView1.SelectedRow.Cells(5).Text
For More Information check the following link
GridView Hidden Column Problem (And Two Common Solutions)
Regards.
Ahmed Naji SharePoint Geek
MCP|MCTS
My English SharePoint Blog | DotnetFinder My Arabic SharePoint Blog| CodeReloaded
My Gallery Contributions
SharePoint 2010 Twitter Web Part
SharePoint 2010 Custom Timer Job
- Edited by Ahmed Naji Saturday, February 4, 2012 7:21 AM Add Reference
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 7:12 AM
All replies
-
This is not exact answer to your question but rather workaround on how to get values from hidden fields. For one of your visible cells, you can create template column with asp:Label for visible value. Add hidden control to this column <asp:HiddenField> and bind desired value. The value is preserved on the page and you can get it via SelectedRow.FindControl("NameOfHiddentField") function.
As far as I know, you cannot retrieve text of the cell from the hidden column.
- Proposed as answer by Nitin J Jain Saturday, February 4, 2012 11:16 AM
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 5:12 AM -
Hi,
Its not possible to get value of hidden bound filed from SPGridView. The reason is SPGridView doesn't render hidden bound fields to the client.
You need to add a Template field as mention below:-
<asp:TemplateField> <ItemTemplate> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("BirthDate") %>' /> </ItemTemplate> <EditItemTemplate> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("BirthDate") %>' /> </EditItemTemplate> </asp:TemplateField>
These hidden template fields can be accessible through code.GridViewRow row = GridView.Rows[e.NewEditIndex]; HiddenField hidden = (HiddenField)row.Cells[0].FindControl("HiddenField1"); // lets HiddenField1 is bound to DateTime type field. DateTime birthDate = Convert.ToDateTime(hidden.Value);
- Proposed as answer by Akhilesh Nirapure Saturday, February 4, 2012 7:10 AM
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 5:39 AM -
Of course you can not get the hidden value from GridView you know why?because the hidden column is not exits.
For example in your situation you have GridView with five column then you set visible=false to the last column ,thats mean at rendering time there will be only four columns not five because you hide the five column.So to solve this problem follow the next steps
1- Click in the GridView then go to the GridView events
2- Double click on the RowCreated event then write this code to hide the GridView column
e.Row.Cells(5).Visible=false 'Hide the column
The above code mean "Generate the GridView with five columns then after creating all columns hide one or more columns based on your requirement"Now you can access to the column normally with no need to create template
Dim somevar = GridView1.SelectedRow.Cells(5).Text
For More Information check the following link
GridView Hidden Column Problem (And Two Common Solutions)
Regards.
Ahmed Naji SharePoint Geek
MCP|MCTS
My English SharePoint Blog | DotnetFinder My Arabic SharePoint Blog| CodeReloaded
My Gallery Contributions
SharePoint 2010 Twitter Web Part
SharePoint 2010 Custom Timer Job
- Edited by Ahmed Naji Saturday, February 4, 2012 7:21 AM Add Reference
- Marked as answer by Shimin Huang Friday, February 17, 2012 8:29 AM
Saturday, February 4, 2012 7:12 AM