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">
<HTML>
<HEAD>
<title>WebForm3</title>
<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">
</HEAD>
<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>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server" ImageUrl="Images/Plus.GIF" CommandName="Expanded"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
<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>
<asp:TemplateColumn>
<HeaderStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></HeaderStyle>
<ItemStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></ItemStyle>
<ItemTemplate>
<asp:PlaceHolder id="Expanded" runat="server" Visible="False">
<TR>
<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>
<Columns>
<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>
</Columns>
</asp:DataGrid>
</asp:PlaceHolder>
</ItemTemplate>
<FooterStyle ForeColor="White" BorderStyle="Dotted" BorderColor="White" BackColor="White"></FooterStyle>
</asp:TemplateColumn>
</Columns>
<PagerStyle BorderColor="Transparent" BackColor="Transparent" Mode="NumericPages"></PagerStyle>
</asp:datagrid></p>
</form>
<P></P>
</FORM>
</body>
</HTML>
========
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.
InitializeComponent()
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
BindData()
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
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(lstrSQL, StrConn)
daAuthor = New SqlDataAdapter(dcAuthor)
daAuthor.Fill(ldtbParentTable)
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)
daTitle.Fill(ldtbChildTable)
ldtbChildTable.TableName = "ChildTable"
Session("dtchild") = ldtbChildTable
'Add both these tables to the dataset
ds.Tables.Add(ldtbParentTable)
ds.Tables.Add(ldtbChildTable)
'//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
ds.Relations.Add(dr)
Catch ex As Exception
ds = Nothing
End Try
'Set the datasource to parent datagrid
DataGrid1.DataSource = ds
DataGrid1.DataBind()
Session("dtsetI") = ds
Catch exc As Exception
'LogMessage(exc)
Exit Sub
Finally
dcAuthor.Dispose()
daAuthor.Dispose()
dcTitle.Dispose()
daTitle.Dispose()
ds.Dispose()
StrConn.Close()
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
BindData()
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"
Else
img.ImageUrl = "Images/Plus.GIF"
End If
exp = e.Item.Cells(5).FindControl("Expanded")
exp.Visible = Not exp.Visible
' Exit For
' End If
'Next
'img = e.Item.Cells(0).FindControl("ImageButton1")
'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("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")
Try
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
DataGrid1.DataBind()
'GroupColumn(dgriddoctor, 0)
sortdtset.Dispose()
Catch ex As Exception
'PageError(ex)
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)
'Try
dview2 = sortdtset.Tables(1)
Dim dview3 As New DataView
dview3.Table = dview2
dgTemp.DataSource = dview3
dgTemp.DataBind()
'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()
dgTemp.DataBind()
'GroupColumn(dgriddoctor, 0)
sortdtset.Dispose()
'Catch ex As Exception
'PageError(ex)
'End Try
End Sub
Public Property gridSortOrder() As String
Get
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
BindData()
End Sub
End Class
================================================== ============