Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
Imports System.Data
Imports System.Data.SqlClient

Partial Class RadGridNested
    Inherits System.Web.UI.Page


    Protected Sub RadGrid2_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource
        'Dim ds As DataSet = DAL.DAL.SelectImportedFiles(0)
        'RadGrid2.DataSource = ds

        Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand("SELECT [OrderID], [OrderDate], [ShipName], [ShipCountry] FROM [Orders]", conn)
        Dim myDataTable As New DataTable
        conn.Open()
        Try
            adapter.Fill(myDataTable)
        Finally
            conn.Close()
        End Try
        RadGrid2.DataSource = myDataTable
    End Sub
    Protected Sub RadGrid2_ItemCreated(sender As Object, e As GridItemEventArgs) Handles RadGrid2.ItemCreated
        If TypeOf e.Item Is GridNestedViewItem Then
            AddHandler TryCast(e.Item.FindControl("ErrorGrid"), RadGrid).NeedDataSource, AddressOf Me.ErrorGrid_NeedDataSource
        End If
    End Sub
    Protected Sub ErrorGrid_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
        Dim nestedItem As GridNestedViewItem = DirectCast(TryCast(sender, RadGrid).NamingContainer, GridNestedViewItem)
        Dim dataKeyValue As String = nestedItem.ParentItem.Item("ImportHeaderID").Text
        If IsNumeric(dataKeyValue) Then
            'Dim ds As DataSet = DAL.DAL.SelectImportedErrors(CInt(dataKeyValue))
            'CType(sender, RadGrid).DataSource = ds

            Dim errors As String() = New String() {"error1", "error2", "error3"}
            sender.DataSource = errors
        End If
    End Sub
    Private Sub RadGrid2_ItemCommand(source As Object, e As GridCommandEventArgs) Handles RadGrid2.ItemCommand
        If e.CommandName = RadGrid.ExpandCollapseCommandName Then
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
            If Not item.Expanded Then
                Dim nestedItem As GridNestedViewItem = DirectCast(item.ChildItem, GridNestedViewItem)
                Dim tempGrid As RadGrid = DirectCast(nestedItem.FindControl("ErrorGrid"), RadGrid)
                tempGrid.Rebind()
            End If
        End If
    End Sub
    Protected Sub RadGrid2_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid2.ItemDataBound
        'If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
        '    Dim ds As DataSet = TryCast(RadGrid2.DataSource, DataSet)
        '    If ds.Tables(0).Rows(DirectCast(e.Item, Telerik.Web.UI.GridDataItem).DataSetIndex)("ImportHeaderState") <> "A" Then
        '        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        '        Dim cellDelete As TableCell = item("DeleteColumn")
        '        cellDelete.Controls.Clear()
        '        Dim img As New Image
        '        Select Case ds.Tables(0).Rows(DirectCast(e.Item, Telerik.Web.UI.GridDataItem).DataSetIndex)("ImportHeaderState")
        '            Case "P"
        '                img.ImageUrl = "~/images/icon-notification-box.gif"
        '            Case "S"
        '                img.ImageUrl = "~/images/icon-notice-box.png"
        '            Case "F"
        '                img.ImageUrl = "~/images/icon_status_fail_26x26.gif"
        '        End Select
        '        cellDelete.Controls.Add(img)
        '    End If
        '    If ds.Tables(0).Rows(DirectCast(e.Item, Telerik.Web.UI.GridDataItem).DataSetIndex)("ImportHeaderState") <> "F" Then
        '        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        '        Dim cellExpand As TableCell = item("ExpandColumn")
        '        cellExpand.Controls.Clear()
        '    End If
        'End If

        If TypeOf e.Item Is GridDataItem Then
            Dim dataItem As GridDataItem = TryCast(e.Item, GridDataItem)
            If TryCast(dataItem.DataItem, DataRowView)("ShipName").ToString().Contains("o") Then
                Dim imageButton As ImageButton = TryCast(dataItem("DeleteColumn").Controls(0), ImageButton)
                imageButton.CommandName = "ExpandCollapse"
                imageButton.ImageUrl = "~/canceled.png"
                imageButton.ToolTip = "View errors"
            Else
                dataItem("ExpandColumn").Controls(0).Visible = False
            End If
        End If
    End Sub
End Class
