Bind only value from KeyValuePair to GridView - c#

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 %>

Related

Accessing Header Text property of GridView TemplateField in the Code Behind when it is set from Header Template

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.

How to populate itemTemplates of TemplateField in gridview from datatable?

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.

Set Value of HiddenField in GridView to new GUID

I have a GridView control with an ItemTemplate that has a HiddenField.
<asp:GridView ID="GridView1" runat="server" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<% Response.Write(Guid.NewGuid()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The problem is that it actually renders exactly <% Response.Write(Guid.NewGuid()) %> instead of a Guid. How do I set the value to render as a Guid and execute the code rather than interpret it as a literal. I've tried using both single quotes ' and double quotes ".
You can try
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value='<%# Guid.NewGuid().ToString() %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to add # In inline coding . Ex : '<%# //code here %>' and
you are setting HiddenField1 value ,so you dont need to use Response.Write . The value should be Value='<%# Guid.NewGuid().ToString() %>' . Hope this will do
You can get the GUID in code behind as follows.
HiddenField HiddenField1 = (HiddenField)row.FindControl("HiddenField1");
Guid guid1;
Guid1.TryParse(HiddenField1.Value, out guid1);
To set the value, you can use
HiddenField1.Value = guid1.ToString();

Cannot set the selectedindexchange of two dropdownlist in gridview

I have three dropdownlist in gridview's template field. If the first dropdownlist has selected value in it then i am populating the other two dropdownlist with an array of listitem. Both the dropdownlist will have same items in them.
Suppose I have dropdown named ddlone, ddltwo, ddlthree. If ddlone has a selectedvalue then according to the selectedvalue of ddlone I am adding items in ddltwo and dllthree. They both have same list of items. The problem is when I set the selectedindex or selectedvalue of either ddltwo or ddlthree then both get the same index or values I don't know how.
Here is my gridview:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false" OnRowDataBound="gridview_RowDataBound">
<HeaderStyle HorizontalAlign="Left" />
<Columns>
<asp:BoundField HeaderText="SrNo" DataField="SrNo" />
<asp:TemplateField HeaderText="ONE">
<ItemTemplate>
<asp:DropDownList ID="ddlone" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlone_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Two">
<ItemTemplate>
<asp:DropDownList ID="ddltwo" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Three">
<ItemTemplate>
<asp:DropDownList ID="ddlthree" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<asp:LinkButton ID="lnkAddon" runat="server" Text="+"
Font-Underline="false">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
All this is being done in gridview_rowdatabound event here is my code:
ddlone.SelectedValue = value_one;
items = GetItems(value_one);
ddltwo.Items.Clear(); ddlthree.Items.Clear();
foreach (ListItem it in items)
{
ddlopen.Items.Add(it); ddlclose.Items.Add(it);
}
if (!string.IsNullOrEmpty(value_two)) ddltwo.SelectedValue = value_two;
if (!string.IsNullOrEmpty(value_three)) ddlthree.SelectedValue = value_three;
When the second last statement is executed the value of ddltwo and ddlthree becomes same. When the last statement is executed the selecetedvalue of ddltwo and ddlthree becomes same.
I tried making a copy of items but still the problem persists.

How can I compare column data with a String in GridView

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>

Categories

Resources