Datagrid Paging with AutoGenerateColumns part 2

M

moondog

Just in case anyone else out there is having the same problem of the
page numbers appearing in one column instead of across the row, here's
how to fix the problem courtesy of (e-mail address removed) from his original
thread of this subject.

Below are the C# and VB.NET code samples of the prerender event of the
datagrid:


C#:

protected void dGrid_PreRender(object sender, EventArgs e)
{
try
{
DataGrid dg = (DataGrid)sender;
//Fix for numbering being in a left aligned column
//For some reason, the ColumnSpan property is ignored and
not rendered
//unless set using the Attributes
if (dg.AllowPaging == true && dg.AutoGenerateColumns ==
true)
{
//Get the Table
System.Web.UI.WebControls.Table tab =
(System.Web.UI.WebControls.Table)dg.Controls[0];
//Change the Top Pager
if (dg.PagerStyle.Position == PagerPosition.Bottom ||
dg.PagerStyle.Position == PagerPosition.TopAndBottom)
{
tab.Rows[tab.Rows.Count -
1].Cells[0].Attributes.Add("colspan",
tab.Rows[1].Cells.Count.ToString());
}
//Change the Bottom Pager
if (dg.PagerStyle.Position == PagerPosition.Top ||
dg.PagerStyle.Position == PagerPosition.TopAndBottom)
{
tab.Rows[0].Cells[0].Attributes.Add("colspan",
tab.Rows[1].Cells.Count.ToString());
}
}
}
catch (Exception ex)
{

}
}



VB.NET:

Private Sub myDatagrid_PreRender(ByVal sender As Object, ByVal e
As System.EventArgs) Handles myDatagrid.PreRender
'Fix for page numbers appearing in one column and expanding
it
'making the datagrid look ugly.

Dim dg As DataGrid = sender

If dg.AllowPaging = True And dg.AutoGenerateColumns = True
Then

'Get the Table
Dim tab As System.Web.UI.WebControls.Table =
dg.Controls(0)
'Change the Top Pager
If dg.PagerStyle.Position = PagerPosition.Bottom Or
dg.PagerStyle.Position = PagerPosition.TopAndBottom Then

tab.Rows(tab.Rows.Count -
1).Cells(0).Attributes.Add("colspan",
tab.Rows(1).Cells.Count.ToString())

'Change the Bottom Pager
If (dg.PagerStyle.Position = PagerPosition.Top Or
dg.PagerStyle.Position = PagerPosition.TopAndBottom) Then

tab.Rows(0).Cells(0).Attributes.Add("colspan",
tab.Rows(1).Cells.Count.ToString())
End If
End If
End If
End Sub
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top