Question

I have gridview with template field that contains a hypelink. I'd like to change the URL of the hyperlink based on some logic in a specific field of my SQL data (data table bound to grid). I'm thinking I need to use the RowDataBound event, check the value of the data table's filed and set the NavigateURL as needed.

How do I access the value of the column in the data table during RowDataBound?

I can figure out if it is not a header or footer row and access the hyper link control with the code below. But drawing a blank on my logic to view the data to determine which URL to set.

if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink hl = (HyperLink)e.Row.FindControl("hlStatus");
    }
Was it helpful?

Solution 2

This seems to do the trick. If there is a better way please let me know.

DataRowView drv = (DataRowView)e.Row.DataItem;
int iStatusCode =  Convert.ToInt32( drv[myDatAccessLayer.Class.Property.ToString()]);

it gives me the value of the column I want in the current row.

OTHER TIPS

The Eval method will give you what you're after.

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        HyperLink hl = (HyperLink)e.Row.FindControl("hlStatus");
        hl.NavigateUrl = "http://www.google.com?q=" + DataBinder.Eval(e.Row.DataItem, "ColumnName");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top