Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
how i could create alabel on gridview based on bool condition value comes from the datasource


i used

<itemtemplate>

<asp:label id="Label1" runat="server" text="<(Convert.ToBoolean(Bind("Gender")))? male:female %> ">




but this didn't work

any suggestions ?
Posted
Updated 28-Apr-10 4:04am
v2

You have to write a code in onrowdatabound event of gridview.
 
Share this answer
 
If you are only concerned about the display, you can insert a function into
the GridView to handle that. In this case, Id use an IIF() as a helper
function:

<asp:label id="Label1" runat="server" text='<%#
iif(Eval("Gender"),"Male","Female") %>'></asp:label>


See the full code below.

Let us know if this helps?


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("Gender", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("Name", GetType(String)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsPostBack Then
GridView1.DataSource = CreateDataSource()
GridView1.DataBind()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Gridview Helper function</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatefield headertext="Name" sortexpression="Name">
<itemtemplate>
<asp:label id="Label2" runat="server" text='<%#
Bind("Name") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Gender"
sortexpression="Gender">
<itemtemplate>
<asp:label id="Label1" runat="server" text='<%#
iif(Eval("Gender"),"Male","Female") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>

</div>
</form>
</body>
</html>



I hope this may help u............... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900