Adding dynamic hyperlink column

I would like to add an Edit column hyperlink dynammically so the user can select it to link to another page and edit the record select.  I have been looking at 2 methods for this but have to come up with any results.  The reson this is dynamic is becuase the data being read in has a dynamic number of columns.  But one column will always be a RecId, which is what I want to pass to the Edit page.

Here are the 2 methods I've been looking into:
1. Read data into a DataTable object.  Then add another column.  I can add another column easily enough.  I set the type as a Hyperlink and then looped through the DataTable, grabing the value of RecId, and appending that to a HyperLink object navigate url like this:
hl.NavigateUrl = "~/web/RecordEdit.aspx?rec=" + hlRec;  // where hlRec is a string of the RecID
After this, I set the the DataTable as the source of a GridView and DataBind for display.  The problem here is that GridView shows all the records except the additional column.  (No suntax or run-time errors were encountered.)

2.  Read data into a DataTable object, the set it as the DataSource for the GridView.  Before DataBind on the GridView, create a HyperLinkField, set the header text and text, and add it to the GridView.  Then DataBind the GridView.  This does add the additional column and it displays, but I'm having troubles setting the NavigateUrl on the HyperLinkField items so that they link to the Edit page, apending the RecId from another column.

I could please use some assistance with either of these methods or if you have another way to accomplish the functionality I'm looking for, that's cool as well.  There isn't a lot of records being returned now (in development) but there probably will be when this gets up and running (probably not over 10,000 or anything but still a 200 or 300 or 1000 is still enough to effect lag here).

I have been all over the web the  last few days and am finding similiar things but nothing quite like what I'm trying to do.  I've also read this result (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23249566.html) but I can quite figure out how to tailor it to what I'm doing.

I'm using ASP.Net 3.5 C#.  Thanks for your help.

LVL 1
nkishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
Referring to your second method:

when you are creating HyperLinkField...set two properties of the field:

HLfF1.DataNavigateUrlFields="RecId";
HLF1.DataNavigateUrlFormatString="EditRecord.aspx?id={0}";

For your GV make sure you have the property set:  DataKeyNames="RecId"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nkishAuthor Commented:
I knew I was really close with that second method... I just couldn't figure out how/where to set the DataNavigateUrlFields property.  I was trying to do it in the GridView RowDataBound or the DataBound events.

Thanks for the help with that last bit.  

I posted the code below for the complete solution.  The only differance is that DataNavigateUrlFields and DataKeyNames are expecting a string[] so I needed to create one.

// dtSiteDate is the DataTable

if (dtSiteData != null)
{
	GV_Data.DataSource = dtSiteData;

	HyperLinkField hlf = new HyperLinkField();
	hlf.HeaderText = "Test";
	hlf.Text = "Edit";
	hlf.DataNavigateUrlFormatString = "~/web/RecordEdit.aspx?r={0}";
	string[] dataFields = new string[] {"Record Id"};
	hlf.DataNavigateUrlFields = dataFields;
	hlf.Visible = true;
	GV_Data.DataKeyNames = dataFields;
	GV_Data.Columns.Add(hlf);

	GV_EntityData.DataBind();
}

Open in new window

0
guru_samiCommented:
Yes that's correct.
Setting DataKeyNames in code-behind will need a string array...
If you set it in .aspx GV markup directly it is just string(comma separated if multiple keys);

DataKeyNames="RecId,Key2,key3"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.