8

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?

David Tunnell
  • 7,252
  • 20
  • 66
  • 124
  • Does this answer your question? [How to hide a column (GridView) but still access its value?](https://stackoverflow.com/questions/5376278/how-to-hide-a-column-gridview-but-still-access-its-value) – Michael Freidgeim Dec 14 '20 at 04:03

6 Answers6

9

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();
    }
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
5

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>
Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
  • 1
    This does remove the text from the boxes but the cells can still be seen. – David Tunnell Jul 01 '13 at 21:46
  • This approach worked for me - I just had to apply the class to both the ItemStyle and the HeaderStyle. (And I need to access the values on the client side, not server side.) – wloescher Nov 13 '14 at 22:58
3

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.

Trevor Tubbs
  • 2,097
  • 16
  • 18
1

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();
user704988
  • 436
  • 1
  • 9
  • 24
1

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

0

try to use logically using font-size

e.g.

grid.Columns[0].HeaderStyle.Font.Size = grid.Columns[0].ItemStyle.Font.Size = 0;
Haseeb Ahmed
  • 235
  • 2
  • 11