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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store the User's datagrid selection

Status
Not open for further replies.

ciscowiz

MIS
Joined
Apr 21, 2004
Messages
146
Location
US
I have a page which has only a datagrid that is populated from a stored procedure. The grid only has one column which is "FileName", basically the names of files. I have set the grid up to populate correctly and set the mouseover so it will highlight the row but I need to store the specific file the user clicks on. Whatever filename they choose I need to store and open a page that will then use that string value. I will include my code just in case. Thanks in advance!

Code:
Dim dgCache As DataView
    Dim dv As DataView

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            
            'Check if ViewSate is already populated
            If IsNothing(ViewState("SortOrder")) Then
                'Assign default column sort order
                GetDataReader("FileName asc")
            Else
                'Else retrieve sort order from ViewState
                GetDataReader(ViewState("SortOrder").ToString())
            End If
        End If



    End Sub

    Sub GetDataReader(ByVal ColumnOrder As String)
        'Assign viewstate to ColumnOrder
        ViewState("SortOrder") = ColumnOrder
        'Set up Cache Object and determine if it exists
        dgCache = CType(Cache.Get("dgCache" & ColumnOrder), DataView)
        If (dgCache Is Nothing) Then



        'Connection Objects
        Dim sql As String = "getreports"
        Dim strConn As String = "server=(local);uid=Reports;pwd=;database=DP;"
        Dim cn As New SqlConnection(strConn)
        cn.Open()
        Dim cmd As New SqlCommand(sql, cn)
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Dim dt As New DataTable("Reports")
        dt.Columns.Add(New DataColumn("FileName"))

        While dr.Read
            Dim drow As DataRow = dt.NewRow
            With drow
                    drow(0) = dr("Description")
            End With
            dt.Rows.Add(drow)
            End While
            'close all connections
            dr.Close()
            cmd.Dispose()
            cn.Dispose()
            'create a dataview and set it to datatable
            Dim dv As DataView = dt.DefaultView
            'Assign column sort order   
            dv.Sort = ColumnOrder

            Cache.Insert("dgCache", dv)
            testGrid1.DataSource = dv
        Else
            'Assign Cached DataView new sort order
            dgCache.Sort = ViewState("SortOrder").ToString()
            'Set webgrid's DataSource to cached object
            testGrid1.DataSource = dgCache
        End If
        testGrid1.DataBind()
        addAttrib()
        Dim s As Object = testGrid1.SelectedItem




    End Sub

    Function SortOrder(ByVal Field As String) As String

        If Field = ViewState("SortOrder").ToString() Then

            SortOrder = Replace(Field, "asc", "desc")

        Else

            SortOrder = Replace(Field, "desc", "asc")

        End If

    End Function
    Private Sub addAttrib()
        Dim strColor As String = "white"
        Dim UserAgent As String = Request.ServerVariables("HTTP_USER_AGENT")
        For Each i As DataGridItem In testGrid1.Items
            i.Attributes.Add("onmouseover", "style.backgroundColor='yellow';style.cursor='hand';")
            i.Attributes.Add("onmouseout", "style.backgroundColor='" & strColor & "';style.cursor='default';")
            'If InStr(1, UserAgent, "Macintosh") > 0 Or InStr(1, UserAgent, "Mac_PowerPC") > 0 Then
            '    i.Attributes.Add("onclick", "window.open('/reports/" & Request.QueryString("coid") & "/" & dv.Table.Rows(i.DataSetIndex)("FileName") & ".rpt?init=html_frame&apsuser=administrator&apspassword=&apsauthtype=secenterprise')")
            'Else
            ' i.Attributes.Add("onclick", "style.color='purple';window.open('/reports/" & Request.QueryString("coid") & "/" & dv.Table.Rows(i.DataSetIndex)("FileName") & ".rpt?init=actx&apsuser=administrator&apspassword=&apsauthtype=secenterprise')")
            'End If
            If strColor = "white" Then
                strColor = "#cccccc"
            Else
                strColor = "white"
            End If
        Next



    End Sub
 
If you add a button to each row, you can use the SelectedIndexChanged to get the filename from the row they selected.

Have a look at for more info


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
I have actually reviewed that code before. So there is no way to extract what the user selects "behind the scenes" so to speak? I dont want 85 buttons on my page, I really just want my list of report files. There must be some way to determine the row the user selected and save it in Session or something.
 
In order for the user to select a row, you must provide them with something to click (you currently only select a row with javascript so there is no way of the server knowing that the row has been clicked as javascript is a client side scripting language).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top