ASP.NET – Programmatically adding a hyperlink field – “Value of type String cannot be converted to ‘1 dimensional array of String’ (vb) – the solution

We came across an interesting problem when trying to programmatically add a column to a asp:gridview using page_load (vb). It took quite a while to figure out the relatively simple solution, so I thought that I would share it

Original code:

Dim hf As New HyperLinkField
hf.Text = "Delete"
hf.DataNavigateUrlFormatString = "delete.aspx?JobID={0}"
hf.DataNavigateUrlFields = "JobID" ‘It is this JobID as the field name to use for the ID that is causing the issues
Me.gridTable.Columns.Add(hf)

Me.gridTable.DataBind()

And the solution:

Dim hf As New HyperLinkField
hf.Text = "Delete"
hf.DataNavigateUrlFormatString = "delete.aspx?JobID={0}"
Dim flds() As String = {"JobID"} ‘Create a new 1-dimensional string array with one item - “Job ID”
hf.DataNavigateUrlFields = flds
Me.gridTable.Columns.Add(hf)

Me.gridTable.DataBind()

If this helps you out, or you have any queries, please do leave me a comment and let me know!

Previous
Previous

PC Pro Podcast 64

Next
Next

PC Pro Podcast 63