471,607 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,607 software developers and data experts.

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:
NullReferenceException error
Any help........pls urgent

================================================== =======
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<p><asp:datagrid id="DataGrid1" runat="server" OnItemCommand="DataGrid1_ItemCommand" Height="128px"
BorderStyle="Dotted" BorderColor="Silver" Width="904px" DataMember="ParentTable" AllowSorting="True"
BackColor="White" HorizontalAlign="Left" Font-Name="Verdana" Font-Size="9pt" AutoGenerateColumns="False"
ItemStyle-VerticalAlign="Top" AllowPaging="True" Font-Names="Verdana" CellPadding="2">
<FooterStyle BackColor="Silver"></FooterStyle>
<ItemStyle BorderColor="Transparent" VerticalAlign="Top"></ItemStyle>
<HeaderStyle Font-Size="10px" Font-Bold="True" HorizontalAlign="Center" BackColor="Silver"></HeaderStyle>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="Images/Plus.GIF" CommandName="Expanded"></asp:ImageButton>
<asp:BoundColumn DataField="au_id" SortExpression="au_id" HeaderText="au_id" FooterText="au_id"></asp:BoundColumn>
<asp:BoundColumn DataField="au_lname" SortExpression="au_lname" HeaderText="au_lname" FooterText="au_lname"></asp:BoundColumn>
<asp:BoundColumn DataField="au_fname" SortExpression="au_fname" HeaderText="au_fname" FooterText="au_fname"></asp:BoundColumn>
<asp:BoundColumn DataField="city" SortExpression="city" HeaderText="city" FooterText="city"></asp:BoundColumn>
<HeaderStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></HeaderStyle>
<ItemStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></ItemStyle>
<asp:PlaceHolder id="Expanded" runat="server" Visible="False">
<TD width="9"></TD>
<TD colspan="4">
<asp:DataGrid id=Datagrid3 DataSource='<%# Container.DataItem.CreateChildView("ParentTable_Ch ildTable") %>' runat="server" OnItemCommand="DataGrid3_ItemCommand" AllowSorting="True" BackColor="Thistle" Font-Name="Verdana" Font-Size="8pt" AutoGenerateColumns="false" ItemStyle-VerticalAlign="Top" AllowPaging="True" CellPadding="2" GridLines="Both" ShowFooter="false" EnableViewState="true" OnSortCommand="DataGrid3_SortCommand" OnItemDataBound="Datagrid3_ItemDatabound" >
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="DimGray"></HeaderStyle>
<asp:BoundColumn DataField="Childau_ID" SortExpression="Childau_ID" HeaderText="au_id" FooterText="au_id"></asp:BoundColumn>
<asp:BoundColumn DataField="title_id" SortExpression="title_id" HeaderText="title_id" FooterText="title_id"></asp:BoundColumn>
<asp:BoundColumn DataField="au_ord" SortExpression="au_ord" HeaderText="au_ord" FooterText="au_ord"></asp:BoundColumn>
<asp:BoundColumn DataField="title" SortExpression="title" HeaderText="title" FooterText="title"></asp:BoundColumn>
<asp:BoundColumn DataField="type" SortExpression="type" HeaderText="type" FooterText="type"></asp:BoundColumn>
<asp:BoundColumn DataField="notes" SortExpression="notes" HeaderText="notes" FooterText="notes"></asp:BoundColumn>
<asp:BoundColumn DataField="pubdate" SortExpression="pubdate" HeaderText="pubdate" FooterText="pubdate"></asp:BoundColumn>
<FooterStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></FooterStyle>
<PagerStyle BorderColor="Transparent" BackColor="Transparent" Mode="NumericPages"></PagerStyle>

Imports System.Data.SqlClient
Public Class WebForm3
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.StrConn = New System.Data.SqlClient.SqlConnection

End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

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

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
Strconn.ConnectionString = ConfigurationSettings.AppSettings.Item("ConnectMe" )
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.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
End If

End Sub

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

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

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

'//Prepare the connection object


'Query to get Customer Details - Parent Query

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


dcAuthor = New SqlCommand(lstrSQL, StrConn)
daAuthor = New SqlDataAdapter(dcAuthor)
ldtbParentTable.TableName = "ParentTable"

'Query to get Order Details - Child Query

lstrSQL = "select t.au_id as Childau_ID,t.title_id,t.au_ord,a.title,a.type,a.no tes,a.pubdate from titleauthor t inner join titles a on t.title_id=a.title_id"

dcTitle = New SqlCommand(lstrSQL, StrConn)
daTitle = New SqlDataAdapter(dcTitle)
ldtbChildTable.TableName = "ChildTable"
Session("dtchild") = ldtbChildTable
'Add both these tables to the dataset

'//Create relation and this relation name should be used on CreateChildView
Dim dr As New DataRelation("ParentTable_ChildTable", ldtbParentTable.Columns("au_id"), ldtbChildTable.Columns("Childau_ID"), False)
dr.Nested = True

Catch ex As Exception
ds = Nothing
End Try

'Set the datasource to parent datagrid

DataGrid1.DataSource = ds
Session("dtsetI") = ds
Catch exc As Exception
Exit Sub
End Try
End Sub
Public Sub DataGrid3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

End Sub
Public Sub DataGrid3_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
Dim dgTemp As New DataGrid
dgTemp = CType(source, DataGrid)
dgUniqueID = dgTemp.UniqueID

End Sub

Public Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand
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.ItemIndex = Session("PrevItemIndex") And dgItem.ItemIndex <> e.Item.ItemIndex Then
img = e.Item.Cells(0).FindControl("ImageButton1")
If img.ImageUrl = "Images/Plus.GIF" Then
img.ImageUrl = "Images/Minus.GIF"
img.ImageUrl = "Images/Plus.GIF"
End If

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

' Exit For
' End If

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

'exp = e.Item.Cells(2).FindControl("Expanded")
'exp.Visible = Not exp.Visible
'Session("PrevItemIndex") = e.Item.ItemIndex 'Store the ItemIndex in session variable
'Session("au_ID") = e.Item.Cells(1).Text 'Store the auID
' DataGrid1.SelectedIndex = e.Item.ItemIndex
End Select
End Sub

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles DataGrid1.SortCommand

sortdtset = Session("dtsetI")
Dim dview As New DataTable
dview = sortdtset.Tables(0)
Dim dview1 As New DataView
dview1.Table = dview
If gridSortOrder = "" Then
gridSortOrder = "ASC"
DataGrid1.ToolTip = "Ascending Sort"
ElseIf gridSortOrder = "ASC" Then
gridSortOrder = "DESC"
DataGrid1.ToolTip = "Decending Sort"
ElseIf gridSortOrder = "DESC" Then
gridSortOrder = "ASC"
DataGrid1.ToolTip = "Ascending Sort"
End If
dview1.Sort = e.SortExpression & " " & gridSortOrder
DataGrid1.DataSource = dview1
'GroupColumn(dgriddoctor, 0)
Catch ex As Exception
End Try
End Sub

Public Sub DataGrid3_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEvent Args)
'Dim sortdtset1 As New DataSet
Dim dview2 As New Data.DataTable
sortdtset = Session("dtsetI")
Dim dgTemp As New DataGrid
dgTemp = CType(source, DataGrid)


dview2 = sortdtset.Tables(1)

Dim dview3 As New DataView
dview3.Table = dview2
dgTemp.DataSource = dview3
'dview2.TableName = "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.SortExpression & " " & gridSortOrder
dgTemp.DataSource = dview3.Table.DataSet
dgUniqueID = dgTemp.UniqueID
Dim a As String
a = dgTemp.Items(0).Cells(0).Text()
'GroupColumn(dgriddoctor, 0)
'Catch ex As Exception
'End Try

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

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex

End Sub
End Class
================================================== ============
Aug 21 '07 #1
0 1907

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Gareth Gale | last post: by
3 posts views Thread by melanieab | last post: by
reply views Thread by Dheeraj Verma via DotNetMonster.com | last post: by
reply views Thread by Dheeraj Verma via DotNetMonster.com | last post: by
8 posts views Thread by Matthew Curiale | last post: by
5 posts views Thread by DKC | last post: by
8 posts views Thread by simchajoy2000 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.