How to bind - DataBinder.Eval(Container.DataItem, ' ') into hyperlink in DataGrid.

In this article I am going to discuss how to use DataBinder.Eval in DataGrid and how to bind more than one properties to any control.

First of all you will learn how to fatch the record from the database and bind to DataGrid in ASP.Net.

Suppose we have a table name 'Puru_test_Employee' into my database like as follows:


Figure 1: My table name Puru_test_Employee in my database.

Step 1: Drag a DataGrid control from toolbar and drop on page or directlly write as follows:

<asp:DataGrid ID="DataGrid1" runat="server"></asp:DataGrid>

Step 2: Write the following code on your default.aspx.cs page

private void ShowFavoriteArticles()


    SqlConnection con = new SqlConnection();

    SqlCommand cmd = new SqlCommand();

    con = new SqlConnection("Data Source=testServer;Initial Catalog=mydatabase; uid=sa;pwd=wintellect");

    cmd.Connection = con;

    cmd.CommandText = "Select * from Puru_test_Employee";


    DataGrid1.DataSource = cmd.ExecuteReader();




And call this method on page_load like as follows:

protected void Page_Load(object sender, EventArgs e)




Output: You will get the following output.


Figure 2:

Now you can see how to bind the DataBinder.Eval as follows:

<asp:DataGrid ID="DataGrid1" runat="server" DataKeyField="EmplayeeId" AutoGenerateColumns="False" ShowHeader="true" CellPadding="5">


    <asp:BoundColumn DataField="EmplayeeId" HeaderText="Emplayee Id" />

    <asp:TemplateColumn HeaderText="Employee Name">


            <asp:HyperLink ID="HyperLinkThread" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"EmployeeName")%>'

    <asp:BoundColumn DataField="ActiveStatus" HeaderText="Active Status" />


And write the following method on default.aspx.cs page

public string GetDetail(object objEmplayeeId, object objEmployeeName, object objActiveStatus)


    int EmplayeeId = int.Parse(objEmplayeeId.ToString());

    string employeename = objEmployeeName.ToString();

    bool ActiveStatus = (bool)objActiveStatus;

    if (ActiveStatus == true)


        return "" + EmplayeeId;

        //Write the url which you want to make



        return "";


Up Next
    Ebook Download
    View all
    View all