Friday 25 April 2014

C# hyperlinks in one column of gridview

Asp.Net hyperlinks in one column of gridview

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridV.aspx.cs" Inherits="testJS.gridV" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView4_RowDataBound">
<Columns>
<asp:BoundField DataField="Sno" HeaderText="Sno" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="GmailApplicable" HeaderText="GmailApplicable" />
<asp:BoundField DataField="YahooApplicable" HeaderText="YahooApplicable" />
<asp:BoundField DataField="FBApplicable" HeaderText="FBApplicable" />
<asp:TemplateField HeaderText="Links">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink ID="hypGmail" runat="server">Gmail</asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:HyperLink ID="hypYahoo" runat="server">Yahoo</asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:HyperLink ID="hypFB" runat="server">FB</asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

---------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace testJS
{
public partial class gridV : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
private void LoadGrid()
{
DataTable dtEmployees = new DataTable();
dtEmployees.Columns.Add("Sno", typeof(int));
dtEmployees.Columns.Add("Name");
dtEmployees.Columns.Add("Age", typeof(int));
dtEmployees.Columns.Add("Salary", typeof(double));
dtEmployees.Columns.Add("GmailApplicable", typeof(bool));
dtEmployees.Columns.Add("YahooApplicable", typeof(bool));
dtEmployees.Columns.Add("FBApplicable", typeof(bool));
DataRow dr;
dr = dtEmployees.NewRow();
dr["Sno"] = 1;
dr["Name"] = "Google";
dr["Age"] = 28;
dr["Salary"] = 3000.00;
dr["GmailApplicable"] = true;
dr["YahooApplicable"] = false;
dr["FBApplicable"] = false;
dtEmployees.Rows.Add(dr);
dr = dtEmployees.NewRow();
dr["Sno"] = 2;
dr["Name"] = "Yahoo";
dr["Age"] = 28;
dr["Salary"] = 3200.00;
dr["GmailApplicable"] = false;
dr["YahooApplicable"] = true;
dr["FBApplicable"] = false;
dtEmployees.Rows.Add(dr);
dr = dtEmployees.NewRow();
dr["Sno"] = 3;
dr["Name"] = "FB";
dr["Age"] = 29;
dr["Salary"] = 8200.00; dr["GmailApplicable"] = false;
dr["YahooApplicable"] = false;
dr["FBApplicable"] = true;
dtEmployees.Rows.Add(dr);
GridView4.DataSource = dtEmployees;
GridView4.DataBind();
}
protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
/* If we set the Visible to false in aspx in Bound column This values will get binded. So we need to set the visibility in code behind*/
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool bgmailApplicable = Convert.ToBoolean(e.Row.Cells[2].Text); //("GmailApplicable");
bool bYahooApplicable = Convert.ToBoolean(e.Row.Cells[3].Text);// .FindControl("YahooApplicable");
bool bFbApplicable = Convert.ToBoolean(e.Row.Cells[4].Text); //.FindControl("FbApplicable");
HyperLink hypGmail = (HyperLink)e.Row.Cells[5].FindControl("hypGmail");
if (bgmailApplicable)
{
hypGmail.NavigateUrl = "http://www.gmail.com";
hypGmail.Visible = true;
}
else
{
hypGmail.Visible = false;
}
HyperLink hypYahoo = (HyperLink)e.Row.Cells[5].FindControl("hypYahoo");
if (bYahooApplicable)
{
hypYahoo.NavigateUrl = "http://www.Yahoo.com";
hypYahoo.Visible = true;
}
else
{
hypYahoo.Visible = false;
}
HyperLink hypFB = (HyperLink)e.Row.Cells[5].FindControl("hypFB");
if (bFbApplicable)
{
hypFB.NavigateUrl = "http://www.fb.com";
hypFB.Visible = true;
}
else
{
hypFB.Visible = false;
}
}
}
}
}