|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSometimes you really need to have a dynamic GridView gv = new GridView () ;
panelGvHolder.Controls.Add( gv ) ;
BackgroundMost of the original code is from the Alex Furmanski's Extended GridView with Insert Functionality. The idea of sorting and paging is from Strong Coders. The formatting idea is from Mike Ellison's Formatting AutoGenerateColumns in an ASP.NET Grid. So herewith, I would like to thank those guys for sharing their brilliant ideas and try to wrap up / bring something additional such as the convenience to have a really GENERIC Ok what is this generic then? Why is it so important? Well keep reading and you will never have to bother remembering and coding those column names from your datatables (when your colleagues from the database side change the database, as long as the name of the datatable stays the same, you will not have to do any recoding. Using the CodeMy development environment is Windows Vista, Microsoft .NET Framework Version 3.5, Microsoft Visual Studio 2008. I have also tested this control on Visual Studio 2005 and .NET 2.0. and Windows XP. So, what to do?! Just download the zip. Unpack it and compile first the See the If you like direct action, just copy paste the following two files - XGrid.cs and Formatter.cs into your App_Code directory and instead of using declarative code ( GUI.Controls.XGrid gv = new GUI.Controls.XGrid ( );
Later on just assign a datatable to it and call So here is the data population method you should change according to your requirements: private void PopulateRds ( )
{
//PUT YOUR CODE HERE TO GET THE DATASET OBJ / DATATABLE OBJ
DataSet rds = new DataSet ( );
DataTable dt = new DataTable ( "myDynamicTable" );
// Create DataColumn instances
DataColumn dId = new DataColumn ( );
DataColumn dName = new DataColumn ( );
DataColumn dDecimal = new DataColumn ( );
dId.ColumnName = "Id";
dId.DataType = Type.GetType ( "System.Int32" );
dName.ColumnName = "Name";
dName.DataType = Type.GetType ( "System.String" );
dDecimal.ColumnName = "Decimal";
dDecimal.DataType = Type.GetType ( "System.Double" );
// Add these DataColumns into the DataTable
dt.Columns.Add ( dId );
dt.Columns.Add ( dName );
dt.Columns.Add ( dDecimal );
for ( int i = 0 ; i< 20 ; i++ )
{
DataRow dr = dt.NewRow ( );
dr ["Id"] = i;
dr ["Name"] = "Name " + System.Convert.ToString ( i );
dr ["Decimal"] = 2222221.3333;
// Add the row into the table
dt.Rows.Add ( dr );
}
rds.Tables.Add ( dt );
Session [ "dt"] = rds.Tables [0];
} //eof method PopulateDataTable
And this is how you create your own extended private void CreateDynamicControls()
{
this.PopulateRds();
for ( int i = 0 ; i<9 ; i++ )
{
GUI.Controls.XGrid gv = new GUI.Controls.XGrid ( );
gv.DataSource = Session ["dt"];
gv.AllowSorting = true;
gv.ShowResultSummary = true;
gv.AllowPaging = true;
gv.ID = i.ToString ( ) + i.ToString();
gv.AutoGenerateColumns = true;
gv.DataBind ( );
panGvHolder.Controls.Add ( gv );
} //eof for method CreateDynamicControls
Oops, I did 9 of those;), but you get the idea - it is really dynamic ... So, yes you do not need to care about the events of one Update for Using Helper Method for Real GridviewI noticed that if you have to deal constantly with fully dynamic public static void PrepListingWithSizeAndIndex ( ref string msg,
ref GUI.Controls.XGrid gv,
ref System.Data.DataSet rds, int flagPageSize , int index)
{
try
{
if ( rds == null || gv == null ||
rds.Tables [0] == null )
{
msg = " No data found ";
//this method just populates an empty one ...
Utils.Data.DataSetGastArbeiter.PopulateMsgDataSet ( ref msg );
gv = new GUI.Controls.XGrid ( );
gv.Visible = true;
gv.DataBind ( );
return;
}
if ( gv != null )
{
//Utils.Debugger.DebugDataSet("From PrepareSimpleListingGV tracking rds ", ref rds);
//| if ( rds.Tables [0] == null )
//{
//generate an empty one
// Utils.Data.DataSetGastArbeiter.GenerateEmptyDataSet ( ref rds );
// }
gv.DataSource = rds.Tables [0];
gv.AutoGenerateColumns = true;
gv.AllowPaging = true;
gv.AllowSorting = true;
gv.ShowResultSummary = true;
//do not do this !!!
//gv.PageIndex = 0; //
gv.PageIndex = index ; //now when this is session variable and paging works!!!!
gv.EnableSortingAndPagingCallbacks = true;
gv.Visible = true;
gv.PageSize = flagPageSize;
gv.DataBind ( );
} //eof if (gv != null )
else
{
throw new Utils.CustomException ( "a null gv was passed !!!" );
}
}
catch ( System.IndexOutOfRangeException io )
{
msg = " No data found ";
Utils.Data.DataSetGastArbeiter.PopulateMsgDataSet ( ref msg );
gv = new GUI.Controls.XGrid ( );
gv.Visible = true;
gv.DataBind ( );
return;
eof catch
} //eof method
Points of InterestI would like to insert a I would like to get the insert functionality in a generic manner (e.g. whatever table from database is used as a datasource to be able to present it and perform edit, insert, update only by knowing its name and using table metadata.... Actually I have created my own version of object - relational mapping applicable for insert, delete, update on a generic gridview, unfortunately it is kind of buggy and not for show ... yet;). So anybody willing to collaborate just mail me - firstname.lastname@gmail.com. Limitations of the ControlThis control has not been tested on a huge load, so before putting on one consider testing...
History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||