Click here to Skip to main content
15,918,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have n number of dynamically generated GridViews in my asp.net page. In all the GridView all the cells are linkbuttons which also dynamically generated. And I want to get selected cells/ linkbuttons corresponding column index. How do i do it?
Note- I dont have GridView name as it is dynamically generated in loop. code as below
VB
Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click
Call GetFinal()
End Sub

Public Sub GetFinal()
 For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()

'DataTable filling Code goes here

GrdView1.DataSource = dt1

AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub

Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

' All cell to LinkButton code goes here

AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub
Posted
Comments
ZurdoDev 7-Mar-14 6:37am    
Just examine e.Row. Put a breakpoint and you should be able to see what it is.

1 solution

Here's one option....

Create a dictionary object to store the names...
VB
' create a dictionary object to store header titles
Private headerNames As Dictionary(Of Int32, String) = Nothing

... and set the names when the RowType is Header. Then when you get to the DataRow you can use the cells index to pull the header name that was stored when the header row was databound.
VB
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.Header Then
        ' initialize dictionary object when header is databound
        headerNames = New Dictionary(Of Int32, String)
        ' iterate header cells and capture each column's index and name and store in the dictionary object
        For i As Int32 = 0 To e.Row.Cells.Count - 1
            headerNames.Add(i, e.Row.Cells(i).Text)
        Next
    End If

    If e.Row.RowType = DataControlRowType.DataRow Then

        For i As Int32 = 0 To e.Row.Cells.Count - 1
            Dim cell As TableCell = e.Row.Cells(i)

            ' get header text from dictionary using column index
            Dim headText As String = headerNames(i).ToString()

            ' code to build link
            '

        Next

    End If

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900