Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I add links to GridView in codebehind?

Status
Not open for further replies.

Cineno

Programmer
Jul 24, 2006
142
US
I have a search box that takes an employee's last name and goes to a results page.

The results page takes the employee's last name from a query string and creates a GridView in code-behind to show all matches.

The GridView is working fine to show all matching employee's last names and first names. I'd like to add a third column to the GridView that contains a link to go to the employee's page. The link should have the employee's ID number in it.

Currently this third column is just showing the ID number, but I'm not sure how to make it a link in code-behind. Here's the code:

Private Sub loadDynamicGrid()

Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String

Dim lastName As String


lastName = Request.QueryString("lastName")

connetionString = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()
sql = "SELECT * FROM [EmployeeList] Where [lastname] like '" & lastName & "%' order by lastname"

connection = New SqlConnection(connetionString)



Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)

'Build Bound Columns
Dim curLastName As New BoundField
curLastName.HeaderText = "Last Name"
curLastName.DataField = "LastName"
GridView3.Columns.Add(curLastName)

Dim curFirstName As New BoundField
curFirstName.HeaderText = "First Name"
curFirstName.DataField = "FirstName"
GridView3.Columns.Add(curFirstName)

'This is the column that I want to add the link to

Dim employeeLink As New BoundField
employeeLink.HeaderText = ""
employeeLink.DataField = "EmplID"
GridView3.Columns.Add(employeeLink)


GridView3.Visible = True
GridView3.DataSource = ds
GridView3.DataBind()

adapter.Dispose()
command.Dispose()
connection.Close()



Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try



End Sub


Thanks for any help!
 
I came up with this solution and I'm getting the links now, but I still need some help displaying them properly.

The columns I showed above are being generated in code-behind. I then added a link field to the GridView on the .aspx page:

<asp:GridView id="GridView3" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display."
AllowSorting="True"
CssClass="GridViewStyle" GridLines="None" Width="100%">
<Columns>

<asp:HyperLinkField DataNavigateUrlFields="EmplID"
DataNavigateUrlFormatString="EmployeeProfile.aspx?EmplID={0}"
DataTextField="EmplID"
DataTextFormatString= "<img src='Images/icons/document-search-result.png' alt='View'/> <u>View</u>" >
<ControlStyle CssClass="titleLinksB" />
<ItemStyle Wrap="False" />
</asp:HyperLinkField>

</Columns>

</asp:GridView>

This works fine, but it's showing the links on the left, and then adding the columns created from code-behind on the right. I'd prefer the link column to be all the way to the right.

Is there a way I can change the column order? I read about VisibleIndex, but I'm using 4.0 and apparently that isn't supported.

Thank you.
 
I got it working. For others needing help with this:

You can use insert instead of add:

GridView3.Columns.Insert(0, curLastName)

Then the code-behind elements will be added first before the columns on the .aspx page.
 
Why are you dynamically adding columns in the first place? Is there a specific reason for this?
 
As the page gets further developed the columns that will load will be based on user selection so I wouldnt know them ahead of time and would need them to be dynamic. I was able to get it working
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top