|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
row count in inherited GridView controlI'm adding a couple of features to the GridView control. I'm not sure how best to get the total row count from the data source to use for displaying page information in the pager rows. Something like this: Records 5 to 10 of 25 I've got this working in the code behind page so now I want to move the code to the new GridView class. The datasource could be anything but is likely to come from a DataTable so I want a reliable count of these rows to use in the class. I've been doing this in the code behind but I'm sure there's a much neater solution. protected void ObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e) { //Response.Write(e.AffectedRows.ToString()); //Response.Write(e.ReturnValue.GetType().ToString()); DataTable dt = (DataTable)e.ReturnValue; totalUsers = dt.Rows.Count; } Can anyone point we in the right direction? Is there any good reference material I should be aware of. Many thanks Andrew Hi J055,
Welcome to the MSDN newsgroup. Regarding on the getting the original returned datasource object from datasource control, I still think using the objectDataSource will be the proper approach. However, if you want to create a cutsom Gridview control and put most code logic in the GridView control itself, I think you should consider override the "PerformSelect" method since this is the one be called when perform databinding(from associated datasourcecontrol). And the "GetData" method is used to get datasourceView from datasourcecontrol(has been implemented by the base databound control): #DataBoundControl.PerformSelect Method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.databound control.performselect(VS.80).aspx #DataBoundControl.GetData Method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.databound control.getdata(VS.80).aspx Also, if you're more concentrating on the pager's customization, you can have a look at the "InitializePager" method which is helpful for customizing pager: #GridView.InitializePager Method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview. initializepager(VS.80).aspx BTW, it'll be helpful to use the reflector tool to inspect the control's code logic , that'll help get a clear view on its structure. Hope this helps. Regards, Steven Cheng Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) Hi Steven
Thanks for the information. The InitializePager method looks useful for overriding in the GridView. I've had a look at it with the Reflector tool and the PerformSelect, GetData methods of the DataBoundControl. I can't see how to get the DataSourceView row count using the PerformSelect or GetData methods from within the derived GridView. Would you be able to give me an example or a few more clues on how I can do this? Thanks again Andrew Show quoteHide quote "Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message news:ALWMOK6UGHA.1764@TK2MSFTNGXA01.phx.gbl... > Hi J055, > > Welcome to the MSDN newsgroup. > > Regarding on the getting the original returned datasource object from > datasource control, I still think using the objectDataSource will be the > proper approach. However, if you want to create a cutsom Gridview control > and put most code logic in the GridView control itself, I think you should > consider override the "PerformSelect" method since this is the one be > called when perform databinding(from associated datasourcecontrol). And > the > "GetData" method is used to get datasourceView from datasourcecontrol(has > been implemented by the base databound control): > > #DataBoundControl.PerformSelect Method > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.databound > control.performselect(VS.80).aspx > > #DataBoundControl.GetData Method > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.databound > control.getdata(VS.80).aspx > > Also, if you're more concentrating on the pager's customization, you can > have a look at the "InitializePager" method which is helpful for > customizing pager: > > #GridView.InitializePager Method > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview. > initializepager(VS.80).aspx > > BTW, it'll be helpful to use the reflector tool to inspect the control's > code logic , that'll help get a clear view on its structure. > > Hope this helps. > > Regards, > > Steven Cheng > Microsoft Online Community Support > > > ================================================== > > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > > ================================================== > > > This posting is provided "AS IS" with no warranties, and confers no > rights. > > > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > > > > > > > > > Hi
I've had a closer look at the PerformSelect and GetData methods of the DataBoundControl using the Reflector tool. I can see that GetData returns a DataSourceView. I Can't see how I can get the actual data from the DataSourceView. There's a Select method and ExecuteSelect abstract method in the DataSourceView but these don't seem to return anything. I think it's really important to be able to access the total row count in the GridView. It's needed to display usful information about where the user is when paging through the GridView. It seems messy to have to pass the total rows from the DataSource into a derived GridView. Is there any other way? Thanks Andrew Hi
I've found a solution which works for me now. The answer was sitting in the GridView.InitializePager method all along. If you want to get the total number (count) of rows in your data source for use in your GridView pager row then you use the pagedDataSource.DataSourceCount property. I've created a derived GridView which contains added page information by overriding the InitializePager method. Here's the code for anyone who wants to do something similar. protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource) { // call the base method first base.InitializePager(row, columnSpan, pagedDataSource); // create a new tablecell to contain the new page information TableCell cell1 = new TableCell(); // divide the pager row in half int ltSpan = (int)(columnSpan / 2); int rtSpan = columnSpan - ltSpan; row.Cells[0].ColumnSpan = rtSpan; // add the new label control with the new page info cell1.Controls.Add(PageInfo(pagedDataSource.DataSourceCount)); // add the new cell to the page row and make some adjustments row.Controls.AddAt(0, cell1); row.Cells[0].ColumnSpan = ltSpan; row.Cells[1].HorizontalAlign = HorizontalAlign.Right; } protected Label PageInfo(int rowCount) { Label label1 = new Label(); int currentPageFirstRow = ((PageIndex * PageSize) + 1); int currentPageLastRow = 0; int lastPageRemainder = (rowCount % PageSize); // if you're on the last page the currentPageLastRow // may be different to the other pages currentPageLastRow = (PageCount == (PageIndex + 1) ? (currentPageFirstRow + lastPageRemainder - 1) : (currentPageFirstRow + PageSize - 1)); label1.Text = String.Format("Records {0} to {1} of {2}", currentPageFirstRow, currentPageLastRow, rowCount); return label1; } Some of the formatting will probably need improving to make it look nice but hopefully this provides a good start. Cheers Andrew Thanks for your response Andrew,
Glad that you've made progress on this. Anyway, please feel free to post here when you need further assistance. Also, if convenient, also welcome to share your new udpates with us :) Regards, Steven Cheng Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
Use windows control in asp.net 2.0
Re: How to get underlying data of TreeView control? (ASP.NET 2.0) Treeview control determining if node expanded or selected stylesheet on ASP.NET 2.0 TreeView and Menu Control ASP.NET 2.0 equivalent for e.Item.ItemIndex ? Design Time Error: Error Rendering Control ValidateEvent bug with table and radio button want to create custom control using graphics Change style of Edit-button in GridView? Bounded Datagrid and Subtotal rows |
|||||||||||||||||||||||