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?
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!
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.
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).
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();
}
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>
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.
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With