David Tunnell
David Tunnell

Reputation: 7542

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

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?

Upvotes: 8

Views: 18552

Answers (6)

Haseeb Ahmed
Haseeb Ahmed

Reputation: 243

try to use logically using font-size

e.g.

grid.Columns[0].HeaderStyle.Font.Size = grid.Columns[0].ItemStyle.Font.Size = 0;

Upvotes: 0

jayesh dhameliya
jayesh dhameliya

Reputation: 101

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

Upvotes: 1

hutchonoid
hutchonoid

Reputation: 33306

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();
    }

Upvotes: 9

user704988
user704988

Reputation: 436

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();

Upvotes: 1

Mikael Engver
Mikael Engver

Reputation: 4768

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>

Upvotes: 5

Trevor Tubbs
Trevor Tubbs

Reputation: 2117

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.

Upvotes: 3

Related Questions