I need to hide content of a column based on comparing its data with a string. I like to do it in Page itself (Page does not have code behind)
For some reason I cannot use Eval or Bind to retrieve data for column. I'm looking for something like,
<asp:GridView ID="GridView1" runat="server" >
<Columns>
<asp:TemplateField>
<%
if ([data from row] == aVarContainingDataToCompare){
Response.Write("Hidden");
} else {
Response.Write([data from row]);
}
%>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate><%# Eval("AnotherData") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Can I do this without using code behind
Is it alright to use following instead? Please note the # sign at the start
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<%# (Eval("TheColumn").ToString() == aVarContainingDataToCompare ? "Hidden": Eval("TheColumn")) %>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
Related
I have a GridView in an asp .net application, where the Header Text of a template field is set in the Header Template of the field, as a label (where it will come from the resource file). Below is the code
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lblNameHeader" Text="Name" runat="server" meta:resourcekey="lblNameHeaderResource1"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>'/>
</ItemTemplate>
</Columns>
</asp:GridView>
In code behind I am trying to access the HeaderText set on the column like this
var headerText = gridView.Columns[0].HeaderText;
But the value is coming Empty and I am not able to retrieve it from the gridView.Columns' HeaderTemplate property as well.
Please help me.
You have several problems with your code.
You are missing a closing </asp:TemplateField>. Your asp:Label is missing a runat="server" attribute.
If you want to use the .HeaderText property, this should be your markup:
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you want to use a HeaderTemplate with custom markup in it, then you need to cast the column to a TemplateField, then access the controls within it.
The table I am querying from has 3 fields, all varchar, but 2 of them are base64String from image and video files. I have added a gridview such:-
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img id="image" runat="server" />
</ItemTemplate>
<ItemTemplate>
<a id="downloadLink" runat="server"></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I searched a lot but have no clue on how to populate the Template field column from a dataTable. I need to set the src attribute of the from database query and also need to set innerHTML for the link.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src='<%#Eval("image_source")%>' runat="server" />
<a id="downloadLink" runat="server"><%#Eval("anchor_content")%></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Where image_source and anchor_content are assumed to be the columns returned by your SQL query.
<asp:SqlDataSource ID="SqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:phiSQL %>"
SelectCommand="select ID, AnalasisDate from ProgTbl "></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDS1" Width="1200px"
AutoGenerateColumns="False" OnRowCommand="GridView1_OnRowCommand"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Identification"/>
<asp:BoundField DataField="AnalasisDate" HeaderText="Time Stamp"/>
...
I need to convert the Analasis date to the the following:
TimeZoneInfo.ConvertTimeFromUtc(result.AnalasisDate.Value, tz).
I was wondering if there is a way to do this in the aspx.cs file so that it shows the ConvertTimeFromUtc version when it displays.
I was wondering if there was something I could do in GridView1_RowDataBound to update the value of AnalasisDate
You can do this in markup with TemplateField:
<asp:TemplateField HeaderText="Time Stamp">
<ItemTemplate>
<%# TimeZoneInfo.ConvertTimeFromUtc(Eval(result.AnalasisDate), GetTimeZone()) %>
</ItemTemplate>
</asp:TemplateField>
Here GetTimeZone is a protected or public method, that should be declared in code behind class and return the necessary time zone.
Alternatively you can leave all the conversion logic to the code behind function, say ConvertFromUtc, and call it inside the <ItemTemplate >:
<asp:TemplateField HeaderText="Time Stamp">
<ItemTemplate>
<%# ConvertFromUtc(Eval(result.AnalasisDate)) %>
</ItemTemplate>
</asp:TemplateField>
In ASPX File
<asp:GridView ID="gvSavedAddresses" runat="server">
<Columns>
<asp:TemplateField SortExpression="AddressType" ItemStyle-Width="9%" HeaderText="Type"
HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:Label runat="server" ID="lblAddressType" Text='<%#Eval("AddressType")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In CS File
objAddr.AddressType = new KeyValuePair<string, string>(dr["AddressTypeLookupID"].ToString(), dr["AddressType"].ToString());
I want to display only Value in the grid. But in my code it is showing both key and value in grid cell. how to avoid this?
Cast the field to its actual type, and retrieve Value:
<%# ((KeyValuePair<string, string>)Eval("AddressType")).Value %>
I am using a gridview Edit to edit the values i have in my gridview, when i press edit, all columns can be edited, i would like that one of the columns is not allowed to be edited.
Is there any way i can do this?
Thiss is my aspx code:
<asp:GridView
ID="GridView1"
runat="server"
AllowSorting="True"
OnRowCommand="GridView1_SelectedIndexChanged1"
AutoGenerateEditButton="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CellSpacing="10"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing">
</asp:GridView>
Thanks
If you are using asp:BoundField, try
<asp:BoundField DataField="CustomerID"
ReadOnly="true"
HeaderText="Customer ID"/>
Or else if you are using asp:TemplateField, you can either
Render it in either an asp:Label inside EditItemTemplate
Omit the EditItemTemplate for that column altogether
Sure, make use of the EditItemTemplate. In the following example field ID will not be edited in the Edit mode:
<asp:GridView runat="server">
<Columns>
...
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<%# Eval("ID") %>
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
please show some markup. Quick and dirty I think depending on your markup aspx you can remove the textbox from the EditItem template for the column you want to prevent editing... there are also other solutions of course :)
If you are using template field
((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;
if boundfield
((BoundField)gvGridView.Columns[index]).ReadOnly = true;