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
}
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
}