row count in inherited GridView control

Discussion in 'ASP .Net Web Controls' started by J055, Mar 29, 2006.

  1. J055

    J055 Guest

    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
     
    J055, Mar 29, 2006
    #1
    1. Advertising

  2. 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.)
     
    Steven Cheng[MSFT], Mar 30, 2006
    #2
    1. Advertising

  3. J055

    J055 Guest

    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/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.)
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    J055, Mar 30, 2006
    #3
  4. J055

    J055 Guest

    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
     
    J055, Mar 31, 2006
    #4
  5. J055

    J055 Guest

    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
     
    J055, Mar 31, 2006
    #5
  6. 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.)
     
    Steven Cheng[MSFT], Apr 3, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H5N1
    Replies:
    0
    Views:
    2,752
  2. Replies:
    5
    Views:
    685
  3. DaveyP

    gridview, paging and row count

    DaveyP, Jun 15, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    907
    DaveyP
    Jun 15, 2007
  4. SAL

    Gridview row count troubles

    SAL, Oct 15, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    707
  5. 7stud --
    Replies:
    11
    Views:
    443
    7stud --
    Nov 9, 2007
Loading...

Share This Page