Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide BoundField's but still be able to get values with C#

Tags:

c#

asp.net

I have a grid view and am using a variety of data:

<asp:BoundField DataField="Catagory" HeaderText="Support Catagory" SortExpression="Catagory" />
<asp:BoundField DataField="AppName" HeaderText="Application Name" SortExpression="IncidentNumber" />
<asp:BoundField DataField="IncidentNumber" HeaderText="Incident #" SortExpression="IncidentNumber" />
<asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="CreatedDate" HeaderText="Created Date" SortExpression="CreatedDate" />
<asp:BoundField DataField="PK_DailyTaskHours" HeaderText="" SortExpression="PK_DailyTaskHours" ReadOnly="true" />
<asp:BoundField DataField="PK_NonScrumStory" HeaderText="" SortExpression="PK_NonScrumStory" ReadOnly="true" />

The last two columns however I don't want to show, I am using it so I can retrieve the primary keys with this C# code:

    string dailyTaskHoursPK = (string)e.Values["PK_DailyTaskHours"].ToString();
    string nonScrumStoryPK = (string)e.Values["PK_NonScrumStory"].ToString();
    SqlDataSource4.DeleteParameters["dailyTaskHoursPK"].DefaultValue = dailyTaskHoursPK;
    SqlDataSource4.DeleteParameters["nonScrumStoryPK"].DefaultValue = nonScrumStoryPK;

However, I don't want to display last two columns. But when I set:

Visible="false"

And try to run the program I get the following error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

What am I doing wrong? How do I prevent the user from seeing those fields?

like image 535
David Tunnell Avatar asked Jul 01 '13 21:07

David Tunnell


People also ask

How to hide the data in Grid view?

The way to hide a column in a DataBound GridView is to trap the RowCreated Event and set a Cell inside the column of the row to Visible = false. Nice!

How to hide a BoundField in GridView?

You can do it by two ways. By hidden field it will easy to maintain to get value by name of hidden field instead of finding cell value in code. 1) Remove the BoundField of ID and add Hidenfield with value as Id which will use for File id on RowDataBound. So change in HTML and Code like below.

How do I hide grid columns?

To hide a Grid column set its Visible parameter to false . To hide a column based on a certain condition you can pass, for example, a ternary operator or a method that returns bool - the app can provide an expression according to its logic (like screen size).


5 Answers

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();
    }
like image 188
hutchonoid Avatar answered Oct 05 '22 20:10

hutchonoid


Try to have them Visible="true", but hide them with css.

<style type="text/css">
 .hidden-field
 {
     display:none;
 }
</style>

...
<asp:BoundField DataField="PK_DailyTaskHours" HeaderText="" SortExpression="PK_DailyTaskHours" ReadOnly="true" >
    <ItemStyle CssClass="hidden-field"/>
</asp:BoundField>
<asp:BoundField DataField="PK_NonScrumStory" HeaderText="" SortExpression="PK_NonScrumStory" ReadOnly="true" >
    <ItemStyle CssClass="hidden-field"/>
</asp:BoundField>
like image 40
Mikael Engver Avatar answered Oct 05 '22 18:10

Mikael Engver


You must also set the DataKeyNames property of the data-bound control. Setting visible to false will cause the fields to not be sent to the client unless the field is specified in the DataKeyNames property. See the msdn page on the DataControlField.Visible Property.

like image 36
Trevor Tubbs Avatar answered Oct 05 '22 19:10

Trevor Tubbs


When you want to hide a column and also get its value, you specify DataKeyNames property for GridView in aspx page.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="PK_DailyTaskHours" ...>

Then you can retrieve that column value as below in code behind.

string showId = (string) GridView1.DataKeys[6].Value.ToString();
like image 42
user704988 Avatar answered Oct 05 '22 20:10

user704988


above code hide BoundField value but not hide headertext and miss match all column so i will some change belove

  <style type="text/css">
        .hidden-field
        {
           display:none;
        }
  </style>

...

<asp:BoundField DataField="PK_NonScrumStory" HeaderText=""  SortExpression="PK_NonScrumStory" ReadOnly="true" ItemStyle-CssClass="hidden-field" HeaderStyle-CssClass="hidden-field" >
</asp:BoundField>

now it's proper work

like image 35
jayesh dhameliya Avatar answered Oct 05 '22 20:10

jayesh dhameliya