Introduction
This article shows you how to create a custom DataGrid with paging, alternate sorting, and is also completely editable
Details
When I was developing some asp.net pages, I lost a lot of time with Datagrids. The code here is to made that work more simple. Only drag the control to the page, complete some properties, point to the methods that controls the updating, deleting and you have a complete editable grid.
[ToolboxData("<{0}:editableElphGrid runat="\""server\">
</{0}:editableElphGrid>")]
[ParseChildren(true)]
public class editableElphGrid : System.Web.UI.WebControls.DataGrid
{
public delegate DataTable getData();
public event getData GetData;
public delegate void updateData(int dataKey,DataGridCommandEventArgs e);
public event updateData UpdateData;
public delegate void deleteData(int dataKey,DataGridCommandEventArgs e);
public event deleteData DeleteData;
public delegate void cancelData(int dataKey,DataGridCommandEventArgs e);
public event cancelData CancelData;
public delegate void editData(int dataKey,DataGridCommandEventArgs e);
public event editData EditData;
private string _cFirst="";
public string FirstSortingField
{
get
{return _cFirst;}
set
{_cFirst=value;}
}
protected override void OnInit(EventArgs e)
{
this.AllowPaging=true;
this.AllowSorting=true;
this.PageIndexChanged+=new DataGridPageChangedEventHandler(
cPage_elphGrid);
this.SortCommand+=new DataGridSortCommandEventHandler(
Order_elphGrid);
this.Load+=new EventHandler(elphGrid_Load);
this.EditCommand+=new DataGridCommandEventHandler(Edit_elphGrid);
this.CancelCommand+=new DataGridCommandEventHandler(Cancel_elphGrid);
this.DeleteCommand+=new DataGridCommandEventHandler(Delete_elphGrid);
this.UpdateCommand+=new DataGridCommandEventHandler(Update_elphGrid);
this.Columns.Clear();
EditCommandColumn col=new EditCommandColumn();
col.ButtonType=ButtonColumnType.LinkButton;
col.CancelText="Cancel";
col.EditText="Edit";
col.UpdateText="Update";
this.Columns.Add(col);
ButtonColumn delCol=new ButtonColumn();
delCol.CommandName="Delete";
delCol.ButtonType=ButtonColumnType.LinkButton;
delCol.Text="Delete";
this.Columns.Add(delCol);
}
private void elphGrid_Load(object sender, EventArgs e)
{
if(!this.Page.IsPostBack)
{
if(this.AllowSorting&&this._cFirst!="")
this.ViewState.Add("_orderBy",this._cFirst);
this.ViewState.Add("_orderType","ASC");
this.DataSource = CreateDataSet();
this.DataBind();
}
}
private void cPage_elphGrid(object sender,
DataGridPageChangedEventArgs e)
{
this.CurrentPageIndex = e.NewPageIndex;
this.DataSource = CreateDataSet();
this.DataBind();
}
private ICollection CreateDataSet()
{
DataTable dt=this.GetData();
if(this.AllowSorting&&this.ViewState["_orderBy"]!=null)
{
if(this.ViewState["_orderType"].ToString() == "ASC")
dt.DefaultView.Sort=(string)
this.ViewState["_orderBy"].ToString()+" ASC";
else if(this.ViewState["_orderType"].ToString()=="DESC")
dt.DefaultView.Sort=(string)this.ViewState["_orderBy"]+
" DESC";
}
return dt.DefaultView;
}
public void Order_elphGrid(object sender, DataGridSortCommandEventArgs e)
{
this.ViewState["_orderBy"]=(string)e.SortExpression;
if(this.ViewState["_orderType"].ToString()=="ASC")
this.ViewState["_orderType"]="DESC";
else if(this.ViewState["_orderType"].ToString()=="DESC")
this.ViewState["_orderType"]="ASC";
this.DataSource = CreateDataSet();
this.DataBind();
}
public void Edit_elphGrid(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=Convert.ToInt32(this.DataKeys[e.Item.ItemIndex]);
this.EditData(id,e);
this.EditItemIndex=e.Item.ItemIndex;
this.DataSource = CreateDataSet();
this.DataBind();
}
public void Delete_elphGrid(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=Convert.ToInt32(this.DataKeys[e.Item.ItemIndex]);
this.DeleteData(id,e);
this.EditItemIndex=-1;
this.DataSource = CreateDataSet();
this.DataBind();
}
public void Update_elphGrid(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=Convert.ToInt32(this.DataKeys[e.Item.ItemIndex]);
this.UpdateData(id,e);
this.EditItemIndex=-1;
this.DataSource = CreateDataSet();
this.DataBind();
}
public void Cancel_elphGrid(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=Convert.ToInt32(this.DataKeys[e.Item.ItemIndex]);
this.CancelData(id,e);
this.EditItemIndex=-1;
this.DataSource = CreateDataSet();
this.DataBind();
}
}
Well, now add the control on a page, and then you must fill the fields .
DataKeyField
: That is the key field of the table, the unique identifier of a row. I put because I use that on Database actions, like delete a specific row etc.
And on the tab of Events :- GetData
, CancelData
, EditData
and DeleteData
.
Finally you should have something like this.
On the aspx page:
<cc1:editableElphGrid id="EditableElphGrid1" runat="server"
DataKeyField="intId"></cc1:editableElphGrid>
On the code behind
protected elphControls.editableElphGrid EditableElphGrid1;
private void InitializeComponent()
{
this.EditableElphGrid1.DeleteData +=
new elphControls.editableElphGrid.eliminarDatos(this.del);
this.EditableElphGrid1.GetData +=
new elphControls.editableElphGrid.obtenerDatos(this.obt);
this.EditableElphGrid1.EditData +=
new elphControls.editableElphGrid.editarDatos(this.edit);
this.EditableElphGrid1.UpdateData +=
new elphControls.editableElphGrid.actualizarDatos(this.act);
this.EditableElphGrid1.CancelData +=
new elphControls.editableElphGrid.cancelarDatos(this.cncl);
this.Load += new System.EventHandler(this.Page_Load);
}
private System.Data.DataTable obt()
{
OleDbConnection conn=new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\\bdProves.mdb");
OleDbDataAdapter adapter=new OleDbDataAdapter("select * from tabla",conn);
adapter.SelectCommand.CommandType=CommandType.Text;
conn.Open();
DataSet ds=new DataSet();
adapter.Fill(ds,"aa");
conn.Close();
conn.Dispose();
return ds.Tables["aa"];
}
private void edit(int dataKey,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private void del(int dataKey,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private void cncl(int dataKey,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private void act(int dataKey,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
Update
Changed the session to save the sorting params for a viewstate, now you can have two grids on the same page. Also Changed the declaration of the first sorting field, now this isn't necessary. Thanks to Zanoza.
Conclusion
I think that this grid is a good base to customize a datagrid. Hope it could be useful. All comments about this are welcomed.