GridView override InitializePager columnSpan problem when using BoundField Visible=false

GridView override InitializePager columnSpan problem when using BoundField Visible=false

Post by J055 » Sat, 10 Jun 2006 05:47:04


Hi

I have an overridden InitializePager method in my custom GridView. I use it
to add some page information to the pager row by splitting the table cell.
This works fine until I add either a BoundField or TemplateField where
visible is set to false.

This causes the two Table Cells of the pager row to have incorrect
columnSpans, e.g. where I have 6 columns and one invisible column (total 7)
the pager row will have a left cell with colspan="6" and a right cell with
colspan="4". If I change then visible attribute to true I get left cell with
colspan="3" and a right cell with colspan="4" which is correct.

I have know idea why this happens. When debugging I have found the
InitializePager method is called with columnSpan parameter set to the number
of columns including the invisible one (7).

Please help.
Thanks
Andrew

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;

// add the new label control with the new page info

cell1.Controls.Add(PageInfo(pagedDataSource.DataSourceCount));

// add the new cell to the pager row and apply the ColumnSpan sizes

row.Controls.AddAt(0, cell1);

row.Cells[0].ColumnSpan = ltSpan;

row.Cells[1].ColumnSpan = rtSpan;

// pager buttons are now the right and page info on the left

row.Cells[1].HorizontalAlign = HorizontalAlign.Right;

}
 
 
 

GridView override InitializePager columnSpan problem when using BoundField Visible=false

Post by J055 » Tue, 13 Jun 2006 04:32:55

Hi

I've taken a different approach to get over the previous problem, however I
now get two new ones. This is the code:

protected override void OnRowCreated(GridViewRowEventArgs e)

{

switch (e.Row.RowType)

{

case DataControlRowType.Header:

if (SortExpression != String.Empty)

DisplaySortOrderImages(SortExpression, e.Row);

break;

case DataControlRowType.Pager:

Context.Trace.Warn("PagerRow");

if (DisplayPageInfo)

{

Table tbl = new Table();

tbl.CellSpacing = 0;

tbl.Width = Unit.Percentage(100);

tbl.Rows.Add(new TableRow());

tbl.Rows[0].Cells.Add(new TableCell());

tbl.Rows[0].Cells.Add(new TableCell());

tbl.Rows[0].Cells[0].Controls.Add(PageInfo(dataSourceRowCount));

tbl.Rows[0].Cells[1].HorizontalAlign = HorizontalAlign.Right;



foreach (Control ctrl in e.Row.Cells[0].Controls)

{

tbl.Rows[0].Cells[1].Controls.Add(ctrl);

}

e.Row.Cells[0].Controls.Add(tbl);

}

break;

default:

break;

}

base.OnRowCreated(e);

}


1. The new table does not get created when there is only one page, i.e. the
pager controls are not present. I still want to display the new table and
the contents of tbl.Rows[0].Cells[0].

2. The VS2005 IDE complains in page design mode - 'There was an error
rendering the control. Object reference not set to an instance of an
object'. However the code runs OK in the browser except for (1).

Even if the above code worked properly I rather have the code in my first
post work to avoid so many nested tables (It's not really how modern web
pages should be designed!).

Please help!
Andrew

 
 
 

GridView override InitializePager columnSpan problem when using BoundField Visible=false

Post by J055 » Tue, 13 Jun 2006 18:39:11

Hello

I've found what was causing the VS IDE to complain. It doesn't like me
writing out

Context.Trace.Warn("debug lines")
in my custom control OnRowCreated method. How would I know that? I still
need help with the other questions raised in my previous post.

Thanks

Andrew
 
 
 

GridView override InitializePager columnSpan problem when using BoundField Visible=false

Post by stchen » Tue, 13 Jun 2006 22:00:50

Hello Andrew,

Thank you for posting.

It seems that you're still struggling with the custom Gridview with custom
paging functionality. As for your 3 questions, I've just done some research
and struggle against the GridView. here are some of the results I got:

1.This does be an existing issue, and based on my reseach upon gridview's
code, when there is only one page needed, the GridView's certain PagerRow
will be set to Visible=False, we can consider set it to true in our
override "CreateChildControls" method. e.g.

========================
protected override void CreateChildControls()
{
base.CreateChildControls();

if(!this.DesignMode)
Context.Response.Write("<br/>Count: " + this.PageCount);

if (this.PageCount == 1)
{

if ((this.PagerSettings.Visible) &&
(this.PagerSettings.Position == PagerPosition.Bottom))
{

BottomPagerRow.Visible = true;

}

}


}
==============================

2. As for the context null reference issue, it is because when in IDE, the
HttpContext is not available, the control is hosted in VS IDE(designer
host). We can simply add code to determine the current context such as

if(Context != null)
{

}

or
if(!this.DesignMode)
{

}

3. As for the further question you mentioned:

================
Even if the above code worked properly I rather have the code in my first
post work to avoid so many nested tables (It's not really how modern web
pages should be designed!).
===============

I'm still not quite clear on this, do you mean that the pager need to be
created each time the control is loaded(in each page request)? And you want
to only create it once?

Please feel free to let me know if there is anything I didn't get.


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.)
 
 
 

GridView override InitializePager columnSpan problem when using BoundField Visible=false

Post by stchen » Fri, 16 Jun 2006 00:04:40

Hi Andrew,

Have you got any further ideas on this issue or does my last reply helps
you a little on this? If there is anything else we can help, please feel
free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

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.)