Sorting Problem in Nested Datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rupalirane07
    New Member
    • Aug 2007
    • 9

    Sorting Problem in Nested Datagrid

    Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error:
    NullReferenceEx ception error
    Any help........pls urgent

    =============== =============== =============== ============
    <%@ Page Language="vb" AutoEventWireup ="false" Codebehind="Web Form3.aspx.vb" Inherits="TestD atagrids.WebFor m3"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm3 </title>
    <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
    <meta content="JavaSc ript" name="vs_defaul tClientScript">
    <meta content="http://schemas.microso ft.com/intellisense/ie5" name="vs_target Schema">
    </HEAD>
    <body MS_POSITIONING= "GridLayout ">
    <form id="Form1" method="post" runat="server">
    <p><asp:datagri d id="DataGrid1" runat="server" OnItemCommand=" DataGrid1_ItemC ommand" Height="128px"
    BorderStyle="Do tted" BorderColor="Si lver" Width="904px" DataMember="Par entTable" AllowSorting="T rue"
    BackColor="Whit e" HorizontalAlign ="Left" Font-Name="Verdana" Font-Size="9pt" AutoGenerateCol umns="False"
    ItemStyle-VerticalAlign=" Top" AllowPaging="Tr ue" Font-Names="Verdana" CellPadding="2" >
    <FooterStyle BackColor="Silv er"></FooterStyle>
    <ItemStyle BorderColor="Tr ansparent" VerticalAlign=" Top"></ItemStyle>
    <HeaderStyle Font-Size="10px" Font-Bold="True" HorizontalAlign ="Center" BackColor="Silv er"></HeaderStyle>
    <Columns>
    <asp:TemplateCo lumn>
    <ItemTemplate >
    <asp:ImageButto n id="ImageButton 1" runat="server" ImageUrl="Image s/Plus.GIF" CommandName="Ex panded"></asp:ImageButton >
    </ItemTemplate>
    </asp:TemplateCol umn>
    <asp:BoundColum n DataField="au_i d" SortExpression= "au_id" HeaderText="au_ id" FooterText="au_ id"></asp:BoundColumn >
    <asp:BoundColum n DataField="au_l name" SortExpression= "au_lname" HeaderText="au_ lname" FooterText="au_ lname"></asp:BoundColumn >
    <asp:BoundColum n DataField="au_f name" SortExpression= "au_fname" HeaderText="au_ fname" FooterText="au_ fname"></asp:BoundColumn >
    <asp:BoundColum n DataField="city " SortExpression= "city" HeaderText="cit y" FooterText="cit y"></asp:BoundColumn >
    <asp:TemplateCo lumn>
    <HeaderStyle ForeColor="Whit e" BorderStyle="Do tted" BorderColor="Wh ite" BackColor="Whit e"></HeaderStyle>
    <ItemStyle ForeColor="Whit e" BorderStyle="Do tted" BorderColor="Wh ite" BackColor="Whit e"></ItemStyle>
    <ItemTemplate >
    <asp:PlaceHolde r id="Expanded" runat="server" Visible="False" >
    <TR>
    <TD width="9"></TD>
    <TD colspan="4">
    <asp:DataGrid id=Datagrid3 DataSource='<%# Container.DataI tem.CreateChild View("ParentTab le_ChildTable") %>' runat="server" OnItemCommand=" DataGrid3_ItemC ommand" AllowSorting="T rue" BackColor="This tle" Font-Name="Verdana" Font-Size="8pt" AutoGenerateCol umns="false" ItemStyle-VerticalAlign=" Top" AllowPaging="Tr ue" CellPadding="2" GridLines="Both " ShowFooter="fal se" EnableViewState ="true" OnSortCommand=" DataGrid3_SortC ommand" OnItemDataBound ="Datagrid3_Ite mDatabound" >
    <AlternatingIte mStyle BackColor="#E0E 0E0"></AlternatingItem Style>
    <HeaderStyle Font-Bold="True" HorizontalAlign ="Center" ForeColor="Whit e" BackColor="DimG ray"></HeaderStyle>
    <Columns>
    <asp:BoundColum n DataField="Chil dau_ID" SortExpression= "Childau_ID " HeaderText="au_ id" FooterText="au_ id"></asp:BoundColumn >
    <asp:BoundColum n DataField="titl e_id" SortExpression= "title_id" HeaderText="tit le_id" FooterText="tit le_id"></asp:BoundColumn >
    <asp:BoundColum n DataField="au_o rd" SortExpression= "au_ord" HeaderText="au_ ord" FooterText="au_ ord"></asp:BoundColumn >
    <asp:BoundColum n DataField="titl e" SortExpression= "title" HeaderText="tit le" FooterText="tit le"></asp:BoundColumn >
    <asp:BoundColum n DataField="type " SortExpression= "type" HeaderText="typ e" FooterText="typ e"></asp:BoundColumn >
    <asp:BoundColum n DataField="note s" SortExpression= "notes" HeaderText="not es" FooterText="not es"></asp:BoundColumn >
    <asp:BoundColum n DataField="pubd ate" SortExpression= "pubdate" HeaderText="pub date" FooterText="pub date"></asp:BoundColumn >
    </Columns>
    </asp:DataGrid>
    </asp:PlaceHolder >
    </ItemTemplate>
    <FooterStyle ForeColor="Whit e" BorderStyle="Do tted" BorderColor="Wh ite" BackColor="Whit e"></FooterStyle>
    </asp:TemplateCol umn>
    </Columns>
    <PagerStyle BorderColor="Tr ansparent" BackColor="Tran sparent" Mode="NumericPa ges"></PagerStyle>
    </asp:datagrid></p>
    </form>
    <P></P>
    </FORM>
    </body>
    </HTML>

    ========
    Imports System.Data.Sql Client
    Public Class WebForm3
    Inherits System.Web.UI.P age

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
    Me.StrConn = New System.Data.Sql Client.SqlConne ction

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.W ebControls.Data Grid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceho lderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeCompo nent()
    Strconn.Connect ionString = ConfigurationSe ttings.AppSetti ngs.Item("Conne ctMe")
    End Sub

    #End Region
    #Region "Global Variables"
    Dim dbas As New DataAccess
    Dim sortdtset As New DataSet
    'Dim CFn As New Class_Fn
    Dim dgUniqueID As String
    Dim dt As New Data.DataTable
    Dim msql As String
    'Public DdlMstrList As Data.DataTable
    'Public DdlPCP As Data.DataTable
    Dim StrConn As SqlConnection
    Public ds As New DataSet
    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    'Put user code to initialize the page here
    If Not Page.IsPostBack Then
    BindData()
    End If

    End Sub

    Private Sub BindData(Option al ByVal astrSort As String = "", Optional ByVal astrSort1 As String = "")

    Dim lstrSQL As String
    Dim ldtbParentTable As New DataTable
    Dim ldtbChildTable As New DataTable

    Dim lobjDataRelatio n As DataRelation
    Dim lintPageSize As Int32
    Dim llngTotalRec As Int32
    'Dim conn
    Dim dcAuthor
    Dim daAuthor
    Dim dcTitle
    Dim daTitle

    Try
    '//Prepare the connection object

    StrConn.Open()


    'Query to get Customer Details - Parent Query


    lstrSQL = "select au_id,au_lname, au_fname,city from authors"

    Try

    dcAuthor = New SqlCommand(lstr SQL, StrConn)
    daAuthor = New SqlDataAdapter( dcAuthor)
    daAuthor.Fill(l dtbParentTable)
    ldtbParentTable .TableName = "ParentTabl e"

    'Query to get Order Details - Child Query


    lstrSQL = "select t.au_id as Childau_ID,t.ti tle_id,t.au_ord ,a.title,a.type ,a.notes,a.pubd ate from titleauthor t inner join titles a on t.title_id=a.ti tle_id"

    dcTitle = New SqlCommand(lstr SQL, StrConn)
    daTitle = New SqlDataAdapter( dcTitle)
    daTitle.Fill(ld tbChildTable)
    ldtbChildTable. TableName = "ChildTable "
    Session("dtchil d") = ldtbChildTable
    'Add both these tables to the dataset
    ds.Tables.Add(l dtbParentTable)
    ds.Tables.Add(l dtbChildTable)

    '//Create relation and this relation name should be used on CreateChildView
    Dim dr As New DataRelation("P arentTable_Chil dTable", ldtbParentTable .Columns("au_id "), ldtbChildTable. Columns("Childa u_ID"), False)
    dr.Nested = True
    ds.Relations.Ad d(dr)


    Catch ex As Exception
    ds = Nothing
    End Try

    'Set the datasource to parent datagrid

    DataGrid1.DataS ource = ds
    DataGrid1.DataB ind()
    Session("dtsetI ") = ds
    Catch exc As Exception
    'LogMessage(exc )
    Exit Sub
    Finally
    dcAuthor.Dispos e()
    daAuthor.Dispos e()
    dcTitle.Dispose ()
    daTitle.Dispose ()
    ds.Dispose()
    StrConn.Close()
    End Try
    End Sub
    Public Sub DataGrid3_ItemD ataBound(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Data GridItemEventAr gs)

    End Sub
    Public Sub DataGrid3_ItemC ommand(ByVal source As Object, _
    ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs)
    Dim dgTemp As New DataGrid
    dgTemp = CType(source, DataGrid)
    dgUniqueID = dgTemp.UniqueID
    BindData()

    End Sub

    Public Sub DataGrid1_ItemC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles DataGrid1.ItemC ommand
    Dim strSQL As String

    Select Case e.CommandName

    Case "Expanded" '//Case for expanding the + sign on Parent grid to show the child grid

    Dim img As ImageButton
    Dim exp As PlaceHolder


    'Before expanding, collapse previously opened child grid
    Dim dgItem As DataGridItem

    'For Each dgItem In DataGrid1.Items
    ' If dgItem.ItemInde x = Session("PrevIt emIndex") And dgItem.ItemInde x <> e.Item.ItemInde x Then
    img = e.Item.Cells(0) .FindControl("I mageButton1")
    If img.ImageUrl = "Images/Plus.GIF" Then
    img.ImageUrl = "Images/Minus.GIF"
    Else
    img.ImageUrl = "Images/Plus.GIF"
    End If

    exp = e.Item.Cells(5) .FindControl("E xpanded")
    exp.Visible = Not exp.Visible

    ' Exit For
    ' End If
    'Next

    'img = e.Item.Cells(0) .FindControl("I mageButton1")
    'If img.ImageUrl = "Images/Plus.gif" Then
    ' img.ImageUrl = "Images/Minus.gif"
    'Else
    ' img.ImageUrl = "Images/Plus.gif"
    'End If

    'exp = e.Item.Cells(2) .FindControl("E xpanded")
    'exp.Visible = Not exp.Visible
    'Session("PrevI temIndex") = e.Item.ItemInde x 'Store the ItemIndex in session variable
    'Session("au_ID ") = e.Item.Cells(1) .Text 'Store the auID
    ' DataGrid1.Selec tedIndex = e.Item.ItemInde x
    End Select
    End Sub

    Private Sub DataGrid1_SortC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles DataGrid1.SortC ommand

    sortdtset = Session("dtsetI ")
    Try
    Dim dview As New DataTable
    dview = sortdtset.Table s(0)
    Dim dview1 As New DataView
    dview1.Table = dview
    If gridSortOrder = "" Then
    gridSortOrder = "ASC"
    DataGrid1.ToolT ip = "Ascending Sort"
    ElseIf gridSortOrder = "ASC" Then
    gridSortOrder = "DESC"
    DataGrid1.ToolT ip = "Decending Sort"
    ElseIf gridSortOrder = "DESC" Then
    gridSortOrder = "ASC"
    DataGrid1.ToolT ip = "Ascending Sort"
    End If
    dview1.Sort = e.SortExpressio n & " " & gridSortOrder
    DataGrid1.DataS ource = dview1
    DataGrid1.DataB ind()
    'GroupColumn(dg riddoctor, 0)
    sortdtset.Dispo se()
    Catch ex As Exception
    'PageError(ex)
    End Try
    End Sub

    Public Sub DataGrid3_SortC ommand(ByVal source As Object, ByVal e As System.Web.UI.W ebControls.Data GridSortCommand EventArgs)
    'Dim sortdtset1 As New DataSet
    Dim dview2 As New Data.DataTable
    sortdtset = Session("dtsetI ")
    Dim dgTemp As New DataGrid
    dgTemp = CType(source, DataGrid)

    'Try

    dview2 = sortdtset.Table s(1)

    Dim dview3 As New DataView
    dview3.Table = dview2
    dgTemp.DataSour ce = dview3
    dgTemp.DataBind ()
    'dview2.TableNa me = "dview3"
    If gridSortOrder = "" Then
    gridSortOrder = "ASC"
    dgTemp.ToolTip = "Ascending Sort"
    ElseIf gridSortOrder = "ASC" Then
    gridSortOrder = "DESC"
    dgTemp.ToolTip = "Decending Sort"
    ElseIf gridSortOrder = "DESC" Then
    gridSortOrder = "ASC"
    dgTemp.ToolTip = "Ascending Sort"
    End If
    dview3.Sort = e.SortExpressio n & " " & gridSortOrder
    dgTemp.DataSour ce = dview3.Table.Da taSet
    dgUniqueID = dgTemp.UniqueID
    Dim a As String
    a = dgTemp.Items(0) .Cells(0).Text( )
    dgTemp.DataBind ()
    'GroupColumn(dg riddoctor, 0)
    sortdtset.Dispo se()
    'Catch ex As Exception
    'PageError(ex)
    'End Try


    End Sub
    Public Property gridSortOrder() As String
    Get
    Dim oSortOrder As Object = ViewState("grid SortOrder")
    If oSortOrder Is Nothing Then
    Return "ASC"
    End If
    gridSortOrder = oSortOrder
    End Get
    Set(ByVal Value As String)
    ViewState("grid SortOrder") = Value
    End Set
    End Property

    Private Sub DataGrid1_PageI ndexChanged(ByV al source As Object, ByVal e As System.Web.UI.W ebControls.Data GridPageChanged EventArgs) Handles DataGrid1.PageI ndexChanged
    DataGrid1.Curre ntPageIndex = e.NewPageIndex
    BindData()

    End Sub
    End Class
    =============== =============== =============== =============== ==
Working...