datagrid in the webpart, some paging problem

S

sean

Hi All,

I'm developing webpart for WSS which is using DataGrid.
The code included below is currently developed under VS2005.
Everything is working, paging, editing, cancel, update.
Then only problem is when the page is the the last page with 2 items on it, or
change PageSize to 2 below, when first item on the page being selected -
pager will be displayed under the first column ( the resulted html does not
have colspan=2).

Could somebody take a close look? The code below is self contained, does not
need anything to run.

Thanks a lot.



using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace LittleGridControl
{
public class LittleGrid : WebPart
{
private DataGrid _dg;
private DataTable _dt;
private DataView _dv;

public void dg_PageIndexChanged(Object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
_dg.EditItemIndex = -1;
_dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

protected void dg_ItemCommand(object source,
DataGridCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "edit":
_dg.EditItemIndex = e.Item.ItemIndex;
BindGrid();
break;
case "cancel":
_dg.EditItemIndex = -1;
BindGrid();
break;
case "update":
_dg.EditItemIndex = -1;
BindGrid();
break;
}
}

private void BindGrid()
{
_dg.DataSource = _dv; // _dv;
_dg.DataBind();
}

private void CreateDataTable()
{
_dt = new DataTable();

_dt.Columns.Add(new DataColumn("Priority", typeof(Int32)));
// Create sample data.
DataRow dr;
for (int i = 1; i < 9; i++)
{
dr = _dt.NewRow();
dr[0] = i;
_dt.Rows.Add(dr);
}
_dv = new DataView(_dt);
}

public void _dg_Load(object sender, EventArgs e)
{
if (_dg.Items.Count == 0)
{
BindGrid();
}
}


protected override void CreateChildControls()
{
Controls.Clear();
_dg = new DataGrid();
_dg.Load += new EventHandler(_dg_Load);
_dg.CellPadding = 0;
_dg.CellSpacing = 0;
_dg.AutoGenerateColumns = false;
_dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
_dg.HeaderStyle.Font.Size = 10; // Unit.Pixel(16);
_dg.HeaderStyle.Font.Bold = true;
_dg.HeaderStyle.BackColor = Color.Black;
_dg.HeaderStyle.ForeColor = Color.White;
string[] headerFonts = { "Verdana", "Arial", "Helvetica",
"sans-serif" };
_dg.HeaderStyle.Font.Names = headerFonts;
_dg.BorderColor = Color.Black;
_dg.BorderWidth = Unit.Pixel(2);
_dg.ShowFooter = false;
_dg.ShowHeader = true;
_dg.AllowPaging = true;
_dg.PageSize = 3;
_dg.PagerStyle.Mode = PagerMode.NumericPages;
_dg.PagerStyle.Position = PagerPosition.Bottom;
_dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;

// Priority

TemplateColumn tc;
tc = new TemplateColumn();
tc.HeaderText = "Priority";
tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
tc.ItemStyle.VerticalAlign = VerticalAlign.Middle;
tc.ItemTemplate = new ItemTemplatePriorityInt(0);
tc.EditItemTemplate = new EditTemplatePriorityInt(0);
_dg.Columns.Add(tc);

// Edit
EditCommandColumn ec = new EditCommandColumn();
ec.ButtonType = ButtonColumnType.LinkButton;
ec.HeaderText = "Actions";
ec.EditText = "Edit";
ec.CancelText = "Cancel";
ec.UpdateText = "Update";
ec.ItemStyle.Wrap = false;
_dg.Columns.Add(ec);
_dg.EditItemIndex = -1;

_dg.PageIndexChanged += new
DataGridPageChangedEventHandler(dg_PageIndexChanged);
_dg.ItemCommand += new
DataGridCommandEventHandler(dg_ItemCommand);

CreateDataTable();

this.Controls.Add(_dg);
this.ChildControlsCreated = true;
}

//protected override void RenderWebPart(HtmlTextWriter writer)

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
_dg.RenderControl(writer);
}
}

#region Item Template Priority for integer
public class ItemTemplatePriorityInt : ITemplate
{
int _column;
public ItemTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
Label l = new Label();
l.DataBinding += new EventHandler(int_DataBinding);
container.Controls.Add(l);
}
private void int_DataBinding(object sender, EventArgs e)
{
Label l = (Label)sender;

DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion


#region Edit Template Priority for integer
public class EditTemplatePriorityInt : ITemplate
{
int _column;
public EditTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
TextBox tbox = new TextBox();
tbox.Columns = 2;
tbox.DataBinding += new EventHandler(tbox_DataBinding);
container.Controls.Add(tbox);
}
private void tbox_DataBinding(object sender, EventArgs e)
{
TextBox tbox = (TextBox)sender;

DataGridItem container = (DataGridItem)tbox.NamingContainer;
tbox.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion

}
 
J

Joe

This is due to bugs in the .NET 2.0 DataGrid, reported here:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=95521

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=167154

In .NET 2.0 some half-baked code has been added to the
PrepareControlHierarchy method to overwrite the ColSpan of the Pager cell.
This results in screwing up the ColSpan for the pager cell in any of the
following cases:

a) The DataGrid contains AutoGenerated colums
b) The DataGrid does not contain a row whose ItemType is ListItemType.Item
(in your case, the last page has two rows with ItemType
ListItemType.SelectedItem and ListItemType.AlternatingItem - no row with
ListItemType.Item).
c) The Pager has been customized. e.g. in an ItemCreated event handler, to
render as a row with more than one cell.


sean said:
Hi All,

I'm developing webpart for WSS which is using DataGrid.
The code included below is currently developed under VS2005.
Everything is working, paging, editing, cancel, update.
Then only problem is when the page is the the last page with 2 items on it, or
change PageSize to 2 below, when first item on the page being selected -
pager will be displayed under the first column ( the resulted html does not
have colspan=2).

Could somebody take a close look? The code below is self contained, does not
need anything to run.

Thanks a lot.



using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace LittleGridControl
{
public class LittleGrid : WebPart
{
private DataGrid _dg;
private DataTable _dt;
private DataView _dv;

public void dg_PageIndexChanged(Object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
_dg.EditItemIndex = -1;
_dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

protected void dg_ItemCommand(object source,
DataGridCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "edit":
_dg.EditItemIndex = e.Item.ItemIndex;
BindGrid();
break;
case "cancel":
_dg.EditItemIndex = -1;
BindGrid();
break;
case "update":
_dg.EditItemIndex = -1;
BindGrid();
break;
}
}

private void BindGrid()
{
_dg.DataSource = _dv; // _dv;
_dg.DataBind();
}

private void CreateDataTable()
{
_dt = new DataTable();

_dt.Columns.Add(new DataColumn("Priority", typeof(Int32)));
// Create sample data.
DataRow dr;
for (int i = 1; i < 9; i++)
{
dr = _dt.NewRow();
dr[0] = i;
_dt.Rows.Add(dr);
}
_dv = new DataView(_dt);
}

public void _dg_Load(object sender, EventArgs e)
{
if (_dg.Items.Count == 0)
{
BindGrid();
}
}


protected override void CreateChildControls()
{
Controls.Clear();
_dg = new DataGrid();
_dg.Load += new EventHandler(_dg_Load);
_dg.CellPadding = 0;
_dg.CellSpacing = 0;
_dg.AutoGenerateColumns = false;
_dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
_dg.HeaderStyle.Font.Size = 10; // Unit.Pixel(16);
_dg.HeaderStyle.Font.Bold = true;
_dg.HeaderStyle.BackColor = Color.Black;
_dg.HeaderStyle.ForeColor = Color.White;
string[] headerFonts = { "Verdana", "Arial", "Helvetica",
"sans-serif" };
_dg.HeaderStyle.Font.Names = headerFonts;
_dg.BorderColor = Color.Black;
_dg.BorderWidth = Unit.Pixel(2);
_dg.ShowFooter = false;
_dg.ShowHeader = true;
_dg.AllowPaging = true;
_dg.PageSize = 3;
_dg.PagerStyle.Mode = PagerMode.NumericPages;
_dg.PagerStyle.Position = PagerPosition.Bottom;
_dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;

// Priority

TemplateColumn tc;
tc = new TemplateColumn();
tc.HeaderText = "Priority";
tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
tc.ItemStyle.VerticalAlign = VerticalAlign.Middle;
tc.ItemTemplate = new ItemTemplatePriorityInt(0);
tc.EditItemTemplate = new EditTemplatePriorityInt(0);
_dg.Columns.Add(tc);

// Edit
EditCommandColumn ec = new EditCommandColumn();
ec.ButtonType = ButtonColumnType.LinkButton;
ec.HeaderText = "Actions";
ec.EditText = "Edit";
ec.CancelText = "Cancel";
ec.UpdateText = "Update";
ec.ItemStyle.Wrap = false;
_dg.Columns.Add(ec);
_dg.EditItemIndex = -1;

_dg.PageIndexChanged += new
DataGridPageChangedEventHandler(dg_PageIndexChanged);
_dg.ItemCommand += new
DataGridCommandEventHandler(dg_ItemCommand);

CreateDataTable();

this.Controls.Add(_dg);
this.ChildControlsCreated = true;
}

//protected override void RenderWebPart(HtmlTextWriter writer)

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
_dg.RenderControl(writer);
}
}

#region Item Template Priority for integer
public class ItemTemplatePriorityInt : ITemplate
{
int _column;
public ItemTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
Label l = new Label();
l.DataBinding += new EventHandler(int_DataBinding);
container.Controls.Add(l);
}
private void int_DataBinding(object sender, EventArgs e)
{
Label l = (Label)sender;

DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion


#region Edit Template Priority for integer
public class EditTemplatePriorityInt : ITemplate
{
int _column;
public EditTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
TextBox tbox = new TextBox();
tbox.Columns = 2;
tbox.DataBinding += new EventHandler(tbox_DataBinding);
container.Controls.Add(tbox);
}
private void tbox_DataBinding(object sender, EventArgs e)
{
TextBox tbox = (TextBox)sender;

DataGridItem container = (DataGridItem)tbox.NamingContainer;
tbox.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion

}
 
J

Joe

The ColumnSpan is initialized correctly when the Pager cell is created, then
screwed up during rendering. A workaround for this bug is to handle the
DataGrid's ItemCreated event as in the example below.
This adds a colspan attribute to the Pager cell with the correct value. The
pager cell is then rendered as a td with two colspan attrubutes, the first of
which is correct. The browser (tested in IE6) ignores the second incorrect
colspan attribute, so the pager is rendered correctly.

protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
if (e.Item.Cells.Count > 0)
{
int columnSpan = e.Item.Cells[0].ColumnSpan;
e.Item.Cells[0].Attributes.Add("colspan", columnSpan.ToString());
}
}
}


sean said:
Hi All,

I'm developing webpart for WSS which is using DataGrid.
The code included below is currently developed under VS2005.
Everything is working, paging, editing, cancel, update.
Then only problem is when the page is the the last page with 2 items on it, or
change PageSize to 2 below, when first item on the page being selected -
pager will be displayed under the first column ( the resulted html does not
have colspan=2).

Could somebody take a close look? The code below is self contained, does not
need anything to run.

Thanks a lot.



using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace LittleGridControl
{
public class LittleGrid : WebPart
{
private DataGrid _dg;
private DataTable _dt;
private DataView _dv;

public void dg_PageIndexChanged(Object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
_dg.EditItemIndex = -1;
_dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}

protected void dg_ItemCommand(object source,
DataGridCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "edit":
_dg.EditItemIndex = e.Item.ItemIndex;
BindGrid();
break;
case "cancel":
_dg.EditItemIndex = -1;
BindGrid();
break;
case "update":
_dg.EditItemIndex = -1;
BindGrid();
break;
}
}

private void BindGrid()
{
_dg.DataSource = _dv; // _dv;
_dg.DataBind();
}

private void CreateDataTable()
{
_dt = new DataTable();

_dt.Columns.Add(new DataColumn("Priority", typeof(Int32)));
// Create sample data.
DataRow dr;
for (int i = 1; i < 9; i++)
{
dr = _dt.NewRow();
dr[0] = i;
_dt.Rows.Add(dr);
}
_dv = new DataView(_dt);
}

public void _dg_Load(object sender, EventArgs e)
{
if (_dg.Items.Count == 0)
{
BindGrid();
}
}


protected override void CreateChildControls()
{
Controls.Clear();
_dg = new DataGrid();
_dg.Load += new EventHandler(_dg_Load);
_dg.CellPadding = 0;
_dg.CellSpacing = 0;
_dg.AutoGenerateColumns = false;
_dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
_dg.HeaderStyle.Font.Size = 10; // Unit.Pixel(16);
_dg.HeaderStyle.Font.Bold = true;
_dg.HeaderStyle.BackColor = Color.Black;
_dg.HeaderStyle.ForeColor = Color.White;
string[] headerFonts = { "Verdana", "Arial", "Helvetica",
"sans-serif" };
_dg.HeaderStyle.Font.Names = headerFonts;
_dg.BorderColor = Color.Black;
_dg.BorderWidth = Unit.Pixel(2);
_dg.ShowFooter = false;
_dg.ShowHeader = true;
_dg.AllowPaging = true;
_dg.PageSize = 3;
_dg.PagerStyle.Mode = PagerMode.NumericPages;
_dg.PagerStyle.Position = PagerPosition.Bottom;
_dg.PagerStyle.HorizontalAlign = HorizontalAlign.Center;

// Priority

TemplateColumn tc;
tc = new TemplateColumn();
tc.HeaderText = "Priority";
tc.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
tc.ItemStyle.VerticalAlign = VerticalAlign.Middle;
tc.ItemTemplate = new ItemTemplatePriorityInt(0);
tc.EditItemTemplate = new EditTemplatePriorityInt(0);
_dg.Columns.Add(tc);

// Edit
EditCommandColumn ec = new EditCommandColumn();
ec.ButtonType = ButtonColumnType.LinkButton;
ec.HeaderText = "Actions";
ec.EditText = "Edit";
ec.CancelText = "Cancel";
ec.UpdateText = "Update";
ec.ItemStyle.Wrap = false;
_dg.Columns.Add(ec);
_dg.EditItemIndex = -1;

_dg.PageIndexChanged += new
DataGridPageChangedEventHandler(dg_PageIndexChanged);
_dg.ItemCommand += new
DataGridCommandEventHandler(dg_ItemCommand);

CreateDataTable();

this.Controls.Add(_dg);
this.ChildControlsCreated = true;
}

//protected override void RenderWebPart(HtmlTextWriter writer)

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
_dg.RenderControl(writer);
}
}

#region Item Template Priority for integer
public class ItemTemplatePriorityInt : ITemplate
{
int _column;
public ItemTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
Label l = new Label();
l.DataBinding += new EventHandler(int_DataBinding);
container.Controls.Add(l);
}
private void int_DataBinding(object sender, EventArgs e)
{
Label l = (Label)sender;

DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion


#region Edit Template Priority for integer
public class EditTemplatePriorityInt : ITemplate
{
int _column;
public EditTemplatePriorityInt(int column)
{
this._column = column;
}
public void InstantiateIn(Control container)
{
TextBox tbox = new TextBox();
tbox.Columns = 2;
tbox.DataBinding += new EventHandler(tbox_DataBinding);
container.Controls.Add(tbox);
}
private void tbox_DataBinding(object sender, EventArgs e)
{
TextBox tbox = (TextBox)sender;

DataGridItem container = (DataGridItem)tbox.NamingContainer;
tbox.Text = ((DataRowView)container.DataItem)[_column].ToString();
}
}
#endregion

}
 

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,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top