Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Paging within nested datagrid... help!!!

Author  Topic 

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-16 : 11:18:39
Hi, I am really struggling with this problem. Has anybody manged to get paging to work within a nested datagrid?

I keep on geting an error of Object reference not set to an instance of an object. The error line is "DataGrid2.CurrentPageIndex = e.NewPageIndex" when I do the page index change

This is what im doing:
'Datagrid2 is nested inside Datagrid1
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton ImageUrl="PlusSign.gif" CommandName="Expand" ID="btnExpand" Runat="server"></asp:ImageButton> 'This is used to call the nested datagrid
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Search Type">
<ItemTemplate>
<asp:Label>
<%# (Container.DataItem("LsSearchType")%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:PlaceHolder ID="ExpandedContent" Visible="False" Runat="server">
<asp:DataGrid ID="Datagrid2" DataSource='<%# filterData(DataBinder.Eval(Container.DataItem, "LsSearchType")) %>' runat="server"
AllowPaging="True" PageSize="10" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Prev" OnPageIndexChanged="DataGrid2_PageIndexChanged">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label><%# Container.DataItem("LsSearchCriteria")%></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


'I use this function to expand the placeholder
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
Select Case e.CommandName
Case "Expand"
Dim ExpandedContent As PlaceHolder = e.Item.Cells(DataGrid1.Columns.Count - 1).FindControl("ExpandedContent")
ExpandedContent.Visible = Not ExpandedContent.Visible

Dim btnExpand As ImageButton = e.Item.Cells(0).FindControl("btnExpand")
If btnExpand.ImageUrl = "../Images/smallPlusSign.gif" Then
btnExpand.ImageUrl = "../Images/smallMinusSign.gif"
Else
btnExpand.ImageUrl = "../Images/smallPlusSign.gif"
End If
End Select
End Sub


'I then use this function to populate the nested datagrid by filtering the Parent data
Protected Function filterData(ByVal intSearchType As Integer)
If IsNothing(Session("source")) = True Then
loadData(dStart, dEnd)
End If
objGapsTB = Session("source")
dv = New DataView(objGapsTB)
dv.RowFilter = "LsSearchType = " & intSearchType
dv.Sort = "LsSearchCriteria"
Return dv
End Function


'I then use this function to page the nested grid
Protected Sub DataGrid2_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
DataGrid2.CurrentPageIndex = e.NewPageIndex
filterData("20") 'repopulate the nested grid, I have hard coded it for now
End Sub

Any help would be much appreciated as im really stuck on this one

Thanks in advance

jhermiz

3564 Posts

Posted - 2005-04-18 : 13:21:49
Try setting the currentpageindex to -1 then to:
e.NewPageIndex after you have reset it.



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-18 : 15:06:44
Hi jhermiz, thanks for the reply.

I have really been pulling my hair out with this one, I tried the following, I hope I applied your method correctly

Protected Sub DataGrid2_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
DataGrid2.CurrentPageIndex = -1
DataGrid2.CurrentPageIndex = e.NewPageIndex
filterData(20)
End Sub

I get the same error "Object reference not set to an instance of an object" on the folloing line:

DataGrid2.CurrentPageIndex = -1

Any other ideas would be much appreciated

Thanks once again
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-04-18 : 15:11:23
Is your grid even binded to a datasource ?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-18 : 16:45:00
Hi,

Yep my datagrid is binded to the datagrid, I use the function filterData(ByVal intSearchType As Integer) to populate the datagrid. But I call this after the DataGrid2.CurrentPageIndex = e.NewPageIndex

Do i have to recraete the datagrid2 some how, I think it maybe a postback problem?? But i dont know how to problem solve it

Go to Top of Page

olay80
Yak Posting Veteran

62 Posts

Posted - 2005-04-19 : 03:30:09
hey if u want my opinion, the windows DataGrid doesnt' have alot of functionality, specially if ur using such complex issues. u could find components that implement this easialy like infragistics web grid, take my word for it.

Oliver
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-04-19 : 10:38:56
I dont agree with olay. Paging is not an issue for a basic datagrid paging function. You could go all out and spend tons of money on components which you then have to re-learn the various methods and programming capabilities of the object. The datagrid is fine for what you want to do especially for paging or sorting.

I have some pretty flexible datagrids so if you search around you will find great resources for datagrids. I don't know why you are recieving the error, have you tried to debug it??? I had the issue of not being able to page a long time ago when I first started ASP.net. I found that setting a break point and debugging it pointed me to the exact problem. My problem was my grid was losing its dataset do me not placing some code in the page load event after a post back. You may have the same problem:

If Not Is PostBack....

Try setting a break point and finding out why this is happening. F11 steps you through the code.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-25 : 14:14:06
Hi Jon/Olay

Thanks for the posts, i have finally got back to this problem. I thought I would let my hair grow back

OK the problem seems like it does not know the page index of Datagrid2 (my nested grid). How can I tell it to assign the DateGridPageIndex in relation to this grid.

Please find below a screen dump of debug window when it hits the PageIndexChanged Sub.



Any help would be so appreciated. Thanks in advance
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-04-25 : 15:55:19
expand e and let us take a look at what "e.NewPageIndex" has.



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-25 : 17:35:14
Hi jhermiz

I pressed to go onto page 2 and e.NewPageIndex = 1

Any ideas?
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2005-04-26 : 06:14:58
Ok here's my take on it.

Since you have one datagrid2 for every row in datagrid1, a statement such as "Datagrid2.CurrentPageIndex = blah", won't work, simply because the compiler does not know which instance of Datagrid2 you are refering to! The event function won't automatically do this for you. But that is exactly why the first parameter of the event ("source") is supplied. That contains a reference to the object that actually raised the event. All you have to do in the event is:


CType(source,Web.UI.WebControls.DataGrid).CurrentPageIndex = e.NewPageIndex


OS
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-26 : 10:05:48
Hi mohdowais

Thanks for the above, I changed my code to the following:

Protected Sub DataGrid2_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
CType(source, Web.UI.WebControls.DataGrid).CurrentPageIndex = e.NewPageIndex
filterData(20)
End Sub

I hope the above is the way you wanted me to try this. I dont get the Object not set to an object error any more but have noticed the following:

1) The data does not change when I click to change a page
2) I have to Click the page counter twice in order for actual page pointer to move
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2005-04-27 : 01:05:35
Oh yes, of course, just that line of code won't work. You'll have to re-bind your datagrid, which I personally find really annoying about the .NET datagrids - you need to bind them to the data source everytime you change the page index. Calling the FilterData function is no help simply because it is being called out of context, i.e. it does not refer to a specific datagrid. I noticed that you have used the function directly in the datasource property of the datagrid2 so you could try calling the DataBind() or Refresh() method (or both!) on the instance that raised the event. Try this:


Dim myGrid as Web.UI.WebControls.DataGrid
myGrid = CType(source, Web.UI.WebControls.DataGrid)
myGrid.CurrentPageIndex = e.NewPageIndex
myGrid.DataBind()
myGrid.Refresh()


OS
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-27 : 07:06:17
Hi mohdowais

Thanks for the above, I tried the above so my code now looks like:

Protected Sub DataGrid2_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Dim Datagrid2 As Web.UI.WebControls.DataGrid
Datagrid2 = CType(source, Web.UI.WebControls.DataGrid)
Datagrid2.CurrentPageIndex = e.NewPageIndex

obj = CType(Session("source"), dsLogs.SearchGapsDataTable)
dv = New DataView(obj)
dv.RowFilter = "LsSearchType = " & 20
dv.Sort = "LsSearchCriteria"
Datagrid2.DataSource = dv
Datagrid2.DataBind()
End Sub

But I get an error on the last line of the sub, the part where it tries to Datagrid2.Databind

I get the following error, "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount. "

Sorry to be such a pain
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2005-04-27 : 07:40:30
Well this error obviously means that this datatable that you are trying to bind to has less rows than the one this datagrid was previously bound to! I see that in your ASPX page, the LsSearchType is being passed from the parent dataset, and you've hard coded it as 20 in your function. Is it possible that a number other than 20 may have been passed to the grid on the first load? Did you try just putting in a DataBind() or Refresh() without all this code to recreate the data source?

OS
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-04-27 : 08:40:57
How about stepping through the debugger as previously mentioned? Step through the debugger and add "watches" to each variable that you are questioning. Press f11 to go line by line.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

BigMeat
Yak Posting Veteran

56 Posts

Posted - 2005-04-27 : 08:46:46
I did try it without populating the Datagrid2 again but I got the same error. I coundnt do Datagrid2.Refresh, is this a Windows App command? VS.Net did not like the syntax

When I load the grid the first time I pass it all the records (1272 rows), I then display this data into 'Search Types'. There are only 3 ways you can search the application. I show the name of each search type and then show the count of each search type, eg like the below:

Search Type (Count)
+ ByContact (125)
+ ByCompany (653)
+ ByEmail (494)
_________________________
Total 1272

The above is basically what Datagrid1 shows, when the user click the '+' sign it them populates Datagrid to show the details of the Search Type. Because there are so many rows I wanted to page them so it cuts down on download time. All the data is loaded when Datagrid1 is populated, so basically Datagrid just shows a filter of the datatable.

Hope this helps
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2005-04-28 : 08:33:43
Hmmm...you should follow Jon's advice and try to step through the entire process...checking the values of the various variables along the way. The key thing for you to check here is the number of rows in the original datasource of datagrid2 and then what happens to that number when that datagrid is paged. I suspect these two datasources are different, maybe you should have another way of associating a SearchType ID with a particular datagrid.

OS
Go to Top of Page
   

- Advertisement -