Hide BoundField, but still be able to get values ​​using C #

I have a grid view and I use various 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 do not want to show, I use it to get 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 do not want to display the last two columns. But when I installed:

 Visible="false" 

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

The reference to the object is not installed in the instance of the object.

Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and its occurrence in the code.

Exception Details: System.NullReferenceException: The object reference was not set to the object instance.

What am I doing wrong? How can I prevent a user from viewing these fields?

+7
source share
6 answers

Trevor is right, you need to set DataKeyNames like this in the DataGrid markup:

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="PK_DailyTaskHours,PK_NonScrumStory" 

Once you do this, you can return the values ​​as strings as follows:

  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(); } 
+6
source share

Try to make 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> 
+5
source share

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

+3
source share

If you want to hide the column and get its value, you specify the DataKeyNames property for the GridView on the aspx page.

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

Then you can get the value of this column, as shown below, in the code.

 string showId = (string) GridView1.DataKeys[6].Value.ToString(); 
+1
source share

the above code hide the value of the BoundField, but do not hide the header text and skip the match with the entire column, so I'll change a little

  <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 the right job

+1
source share

try using logically using font size

eg.

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

All Articles