Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > row count in inherited GridView control

Reply
Thread Tools

row count in inherited GridView control

 
 
J055
Guest
Posts: n/a
 
      03-29-2006
Hi

I'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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      03-30-2006
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/lib...rols.databound
control.performselect(VS.80).aspx

#DataBoundControl.GetData Method
http://msdn2.microsoft.com/en-us/lib...rols.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/lib...trols.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.)










 
Reply With Quote
 
 
 
 
J055
Guest
Posts: n/a
 
      03-30-2006

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



"Steven Cheng[MSFT]" <> wrote in message
news:...
> 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/lib...rols.databound
> control.performselect(VS.80).aspx
>
> #DataBoundControl.GetData Method
> http://msdn2.microsoft.com/en-us/lib...rols.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/lib...trols.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.)
>
>
>
>
>
>
>
>
>
>



 
Reply With Quote
 
J055
Guest
Posts: n/a
 
      03-31-2006
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


 
Reply With Quote
 
J055
Guest
Posts: n/a
 
      03-31-2006
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.DataSo urceCount));

// 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


 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-03-2006
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.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Gridview row databound event - can't get past the 1st row of gridview maurban@gmail.com ASP .Net 5 10-13-2006 09:37 PM
GridView nested in DataList - refreshing corresponding DataList row after updating GridView row H5N1 ASP .Net 0 04-26-2006 11:41 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments