Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have created a dynamic grid view, i have four fields in it namely:-Name, Gender, Contact, Email. I want to make the name field as a link from which i can open a new page which will show complete profile of person.

Please help me with this.
Posted

Updated:
Adding Hyperlink dynamically into a dynamic added GridView
C#
private void NewMethod()
{
    DataTable dt = SomeMethodReturnsDataTable();

    GridView gv = new GridView();
    PlaceHolder1.Controls.Add(gv);

    gv.DataSource = dt;
    gv.DataBind();

    foreach (GridViewRow gr in gv.Rows)
    {
        HyperLink hp = new HyperLink();
        hp.Text = gr.Cells[0].Text;
        hp.NavigateUrl = "~/Default.aspx?name=" + hp.Text;
        gr.Cells[0].Controls.Add(hp);
    }
}

C#
private DataTable SomeMethodReturnsDataTable()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = "Data Source=.; Initial Catalog=skand; Integrated Security=true";
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select candidatename as 'Name', gender as 'Gender', contactmobile as 'Contact Number', contactmail as 'E-Mail' from tblUserRegister";

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        con.Close();
    }
    return dt;
}


-------------------------------------------------------------
Code for adding hyperlink directly into ASP.NET tag page
ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
    <columns>
        <asp:hyperlinkfield datatextfield="name" headertext="Name" />
    </columns>
</asp:gridview>

code behind
C#
protected void Page_Load(object sender, EventArgs e)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("name");
    dt.Rows.Add("Steve Jobs");
    dt.Rows.Add("Bill Gates");

    GridView1.DataSource = dt;
    GridView1.DataBind();

    foreach (GridViewRow gvr in GridView1.Rows)
    {
        ((HyperLink)gvr.Cells[0].Controls[0]).NavigateUrl = "~/Default.aspx?name=" 
                                              + dt.Rows[gvr.RowIndex]["name"];
    }
}
 
Share this answer
 
v3
Comments
Skand Pratap Singh 25-Oct-12 2:18am    
Mr.Adriancs, I found your reply quite fitting to my question. But I want to make an already existing column as link. And I am not creating that column on my own, I am deriving it from an Sql table namely:-"candidate name" What should I do in that case?? Please find the code attached and tell me what i can do with it...

GridView gridview = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(gridview);
gridview.HeaderStyle.BackColor = Color.Black;
gridview.HeaderStyle.ForeColor = Color.White;
gridview.AlternatingRowStyle.BackColor = Color.MediumAquamarine;
gridview.AlternatingRowStyle.ForeColor = Color.Black;
gridview.CellPadding = 7;
gridview.AllowPaging = true;
gridview.PageSize = 3;
gridview.PageIndexChanging += new GridViewPageEventHandler(gridview_PageIndexChanging);

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.; Initial Catalog=skand; Integrated Security=true";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Select candidatename as 'Name', gender as 'Gender', contactmobile as 'Contact Number', contactmail as 'E-Mail' from tblUserRegister";

//cmd.CommandText = "Select * FROM tblUserRegister WHERE DATE BETWEEN '" +TextBox.TEXT.TOdATEtIME() + "' AND '' ";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);


gridview.DataSource = ds;
gridview.DataBind();
con.Close();
}

protected void gridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridview.PageIndex = e.NewPageIndex;
}
adriancs 25-Oct-12 7:13am    
Hi, I have reply in the above solution.
 
Share this answer
 
 
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