1

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");
    }
RichP
  • 525
  • 1
  • 10
  • 25

2 Answers2

0

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");
    }
}
Red Taz
  • 4,159
  • 4
  • 38
  • 60
  • thanks but I know how to set all the parts of the hyperlink I am trying to figure out what the underlying data of that current row's column is so I can make a decision. The grid is bound to a DataTable that has rows. I need access to one column of that binding row so I can run if or case logic on it. – RichP Dec 19 '12 at 16:32
  • Looking closer to your solution I see you have the e.Row.DataItem on the end of the URL Kind of what I needed but not as part of the URL. Thanks none the less! – RichP Dec 19 '12 at 16:44
0

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.

RichP
  • 525
  • 1
  • 10
  • 25