How to hide a column (GridView) but still access its value?
If I am not mistaken, GridView
does not hold the values of BoundColumns
that have the attribute visible="false"
. Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys
. Or you can change your BoundColumn
to a TemplateField
. And in the ItemTemplate
, add a control like Label
, make its visibility false and give your value to that Label
.
GridView - Hide Column but Use Value
Here's a general overview of what you can do. It's not complicated. You databind as usual, but while doing that you find the index of the ID column. Then you use that index to hide the column on the RowCreated event. If you search for how to hide a column using auto-generated columns, you'll run into several answers following the method I'm using for hiding.
Private IDColumnIndex As Integer //Class scope - we need to use it in multiple methods
Public Sub PageLoad() Handles Me.Load //or wherever your databind is happening
Dim source As New DataTable()
IDColumnIndex = source.Columns("id").Ordinal //Gets column index of "ID" column
view.DataSource = source
view.DataBind()
End Sub
//Hide our "ID" auto-generated column.
Public Sub view_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles view.RowCreated
e.Row.Cells(IDColumnIndex).Visible = False
End Sub
How to hide a column but still access its value?
Set this code after you've binded the data. To get this functionality I do this:
MyGridView.Columns[0].visible = true;
MyGridView.DataBind();
MyGridView.Columns[0].visible = false;
With this the first column is hidden, but you should be able to acces it's value.
Getting cell value from hidden column
Instead of hiding that cell, use a TemplateField
that contains an ASP.NET HiddenField
control, like this:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenFieldDifferentUsers" Value='<%# Eval("DifferentUsers") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
Now in your code-behind, you can find the hidden field control, like this:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField theHiddenField = e.Row.FindControl("HiddenFieldDifferentUsers") as HiddenField;
// Check that we successfully found hidden field before using it
if(theHiddenField != null)
{
// Do something with hidden field here if you need to
}
}
}
Hide BoundField's but still be able to get values with C#
Trevor is correct, you need to set your DataKeyNames
like this in your DataGrid markup:
<asp:GridView ID="GridView1" runat="server"
DataKeyNames="PK_DailyTaskHours,PK_NonScrumStory"
Once you have done this you can get the values back as strings like this:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string dailyTaskHoursPK = GridView1.DataKeys[0].Values["PK_DailyTaskHours"].ToString();
string nonScrumStoryPK = GridView1.DataKeys[0].Values["PK_NonScrumStory"].ToString();
}
how to hide a column in gridview based on a condition?
You can hide columns in code behind.
if (User.IsInRole("Admin") == false)
{
GridView1.Columns[0].Visible = false;
}
Or as one-liner.
GridView1.Columns[0].Visible = !User.IsInRole("Admin");
Get the value of hidden column in Gridview
One way I have gotten values from Invisible GridView Columns is using the DataKeyNames attribute.
<asp:GridView runat="server" ID="GridView" DataKeyNames="ColName1, ColName2">
</asp:GridView>
then to access the data
var data = GridView.DataKeys[RowIndex].Values[KeyIndex]
Make Gridview column invisible, but still access its data
Wherever you want to use the column value just use <%# Eval("ColumnName") %>
inside the GridView.
All the datasource (dataset in your case) values are available in the DataBinder within the context of the GridView.
Related Topics
Automapper Convert from Multiple Sources
Getting/Setting File Owner in C#
Using the Null-Conditional Operator on the Left-Hand Side of an Assignment
Displaying Arabic Characters in C# Console Application
Could Not Load File or Assembly ... the Parameter Is Incorrect
What Represents a Double in SQL Server
404 Error After Adding Web API to an Existing MVC Web Application
Raise Event Thread Safely - Best Practice
Generics - Where T Is a Number
Web API How to Add a Header Parameter for All API in Swagger
Execute Specified Function Every X Seconds
How to Invoke a UI Method from Another Thread
"Parameter Not Valid" Exception Loading System.Drawing.Image