Re: row count in inherited GridView control



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


.



Relevant Pages

  • Re: SMACK netfilter smacklabel socket match
    ... Processes cannot be assigned the internet label. ... struct netlbl_audit *audit_info) ... int ret_val; ... * How communications on this socket are treated. ...
    (Linux-Kernel)
  • TVM_GETITEM does not return unicode label
    ... When I am trying to get an item's label using TVM_GETITEM API message, ... private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int ... public int hItem; ... private const int TV_FIRST = 0x1100; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Non-modifiable index/field values
    ... Martin. ... > reject any row that matches another row in the table on the shadow key ... > pk1 int not null, ... > select @@rowcount ...
    (microsoft.public.sqlserver.programming)
  • Re: REFERENCES REVEALED
    ... > int main ... that the same is not true of pointers. ... address of that object with that label. ... It's good that you have something in mind that gives you a better ...
    (comp.lang.cpp)
  • Re: @@ROWCOUNT help!!!
    ... > create proc TestRowCount(@check int) as> ... > But somehow @@rowcount gets set to 0 after the IF-ELSE block in the SP> even though checking @@rowcount is the immediate statement after the> update??? ... And it's probably> related to the temp table in caller. ...
    (microsoft.public.sqlserver.programming)

Quantcast