473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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">
<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
================================================== ============
Aug 21 '07 #1
0 2043

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

Similar topics

4
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
3
by: melanieab | last post by:
Hi, I'm programatically sorting in a datagrid. When a column header is clicked, the sort happens twice for some reason, making it looks like it only sorts in descending order. I can tell it...
0
by: Dheeraj Verma via DotNetMonster.com | last post by:
Hi all, I have a asp data grid under which i have another datagrid which is my user control and is autogenerated.Now I am not able to implement sorting in my inner datagrid eventhough I have...
0
by: Dheeraj Verma via DotNetMonster.com | last post by:
Hi all, I have a asp data grid under which i have another datagrid which is my user control and is autogenerated.Now I am not able to implement sorting in my inner datagrid eventhough I have...
8
by: Matthew Curiale | last post by:
I am creating an app that lists clients of a company for management of different attributes for that company. The first page is a listing of the companies currently in the database. I have my...
5
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but...
8
by: simchajoy2000 | last post by:
I thought the only thing I had to do to disable column sorting in VB.NET was to set datagrid.AllowSorting = False. Unfortunately this has never worked for me. I discovered another set of code...
0
by: Chris | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.