Issues with 1.1 to 4.5 migration with databinding of datagrid custom control with ITemplate

I am working on one of my project where we are migrating from 1.1 to 4.5. Here I am facing an issues with custom datagrid databinding.  In the custom datagrid we are using Template columns which is used by implementing ITemplate interface. In aspx.cs file all we are simply doing is

mydatagrid.DataSource  = myArrayList;

The  problem is whenever  after mydatagrid.DataBind() its not updating the DataGridItemCollection with latest item collection, even the data source is with latest data. Below is some part of the custom datagrid code .  
[In the ItemDataBound even of AsDataGrid there is nothing much just base.ItemDataBound(e);]

Is it the compatibility issue with later version ?

Please give me your inputs.
[assembly:TagPrefix( "AsWebControls", "aswc" )]

namespace WebControls {
   public class AsCodeDataGrid : AsDataGrid {

      private string          _selectedCodeValue;

      public string SelectedCodeValue {

         get { return _selectedCodeValue; }
         set { _selectedCodeValue = value; }


      /// Binds the DataGrid to the DataSource

      public override void DataBind() {
         try {
         catch( Exception caughtException ) {
      private void FormatCodeDataGrid() {

         // When paging is enabled this routine may be called multiple times.
         // The IF-statement ensures the columns appear once.
         if( base.Columns.Count > 0 ) {

         // Configure DataGrid's Properties
         base.AutoGenerateColumns = false;
         base.CellSpacing = 0;
         base.CellPadding = 0;
         base.CssClass = "styleDialog";
         base.BorderWidth = 1;
         base.BorderColor = ColorTranslator.FromHtml( AsDataGrid.COLOR_DataGridBorder );
         base.HeaderStyle.CssClass = "styleColumn";
         base.Page.ClientScript.RegisterClientScriptBlock(GetType(), "HighlightRowBlock", JScriptGenerator.BuildHighLightRowBlock() );
         base.Page.ClientScript.RegisterClientScriptBlock(GetType(), "UnhighlightRowBlock", JScriptGenerator.BuildUnhighLightRowBlock());

         // Check whether the Grid is to be populated
         if( base.DataSource == null ) {
            IsPopulated = false;
         else {
            IsPopulated = true;

         // Add the columns to the grid
         base.Columns.Add( CreateColumn( base.RetrieveCosmeticSubstitution( "Code Value" ), "CodeValue", 75 ) );
         base.Columns.Add( CreateColumn( base.RetrieveCosmeticSubstitution( "Short Description" ), "ShortDescription", 220 ) );
         base.Columns.Add( CreateColumn( base.RetrieveCosmeticSubstitution( "Long Description" ), "LongDescription", 395 ) );
         base.Columns.Add( CreateCheckBoxImageColumn() );
         base.Columns.Add( CreateCodeImageGroupColumn() );

         // Create Header row if no Status exists
         if( base.DataSource == null ) {
            DataTable emptyDataSource = new DataTable();
            emptyDataSource.Columns.Add( "CodeValue" );
            emptyDataSource.Columns.Add( "ShortDescription" );
            emptyDataSource.Columns.Add( "LongDescription" );
            emptyDataSource.Columns.Add( "System" );
            emptyDataSource.Rows.Add( emptyDataSource.NewRow() );
            base.DataSource = emptyDataSource;

      private TemplateColumn CreateCheckBoxImageColumn() {

         TemplateColumn checkBoxColumn = new TemplateColumn();
         ArrayList CodeGridDclList = null;

         CodeGridDclList = (ArrayList)((AdminServer.WebControls.AsDataGrid)this).DataSource;
         // Configure the column
         checkBoxColumn.HeaderStyle.Width = 70;
         checkBoxColumn.ItemTemplate = new CheckBoxTemplate(CodeGridDclList);
         checkBoxColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
         checkBoxColumn.ItemStyle.Width = 70;
         checkBoxColumn.HeaderText = "System";
         return checkBoxColumn;


      private TemplateColumn CreateCodeImageGroupColumn() {

          TemplateColumn codeImageGroupColumn = new TemplateColumn();

          ArrayList CodeGridDclList = null;

          CodeGridDclList = (ArrayList)((AdminServer.WebControls.AsDataGrid)this).DataSource;

         //Configure the column
         codeImageGroupColumn.HeaderStyle.Width = 45;
         if( IsPopulated ) {
            codeImageGroupColumn.HeaderText = base.CalculateItemCount().ToString();
         codeImageGroupColumn.HeaderStyle.ForeColor = ColorTranslator.FromHtml( AsDataGrid.COLOR_DataGridHeaderStyleFore );
         codeImageGroupColumn.ItemTemplate = new CodeImageGroupTemplate(_selectedCodeValue, base.ButtonClickHandler, base.IsPopulated, base.ShowHeader, base.ClientID, 		base.PageSize, base.CurrentPageIndex, AuthorizedButtonList, CodeGridDclList);
         codeImageGroupColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
         codeImageGroupColumn.ItemStyle.Width = 45;
         return codeImageGroupColumn;


      private class CheckBoxTemplate : AsItemTemplate {

          private ArrayList _codeGridDclList;

          public CheckBoxTemplate(ArrayList CodeGridDclList)
              _codeGridDclList = CodeGridDclList;

         public override void InstantiateIn( Control container ) {

            // Create and configure the CheckBox Image
            System.Web.UI.WebControls.Image checkBoxImage = new System.Web.UI.WebControls.Image();
            checkBoxImage.ID = "CheckBoxImage";
            checkBoxImage.BorderWidth = 0;
            checkBoxImage.EnableViewState = false;
            checkBoxImage.ImageUrl = "../images/AsIconCheck.gif";
            checkBoxImage.DataBinding += new EventHandler( CheckBoxImage_DataBinding );
            container.Controls.Add( checkBoxImage );


         private void CheckBoxImage_DataBinding( object sender, System.EventArgs e ) {

            System.Web.UI.WebControls.Image checkBoxImage = ( System.Web.UI.WebControls.Image )sender;
            DataGridItem currentGridRow = ( DataGridItem )checkBoxImage.NamingContainer;
          //  CodeDcl codeDcl = ( CodeDcl )currentGridRow.DataItem;
            if( codeDcl.SystemIndicator == "1"  ) {
               checkBoxImage.Visible = true;
            else {
               checkBoxImage.Visible = false;



      private class CodeImageGroupTemplate : AsItemTemplate {

         private string                _selectedCodeValue;
         private ArrayList _codeGridDclList;

         public CodeImageGroupTemplate( string selectedCodeValue, CommandEventHandler buttonClickHandler, bool isPopulated, bool isDataGridHeaderVisible, string dataGridClientId, int dataGridPageSize, int dataGridCurrentPageIndex, ArrayList authorizedButtonList , ArrayList CodeGridDclList) {

            _selectedCodeValue = selectedCodeValue;
            this.ButtonClickHandler = buttonClickHandler;
            this.IsPopulated = isPopulated;
            this.IsDataGridHeaderVisible = isDataGridHeaderVisible;
            this.DataGridClientId = dataGridClientId;
            this.DataGridPageSize = dataGridPageSize;
            this.DataGridCurrentPageIndex = dataGridCurrentPageIndex;
            this.AuthorizedButtonList = authorizedButtonList;
            _codeGridDclList = CodeGridDclList;

         public override void InstantiateIn( Control container ) {

            // Create and configure the Select Image button
            ImageButton editImageButton = new ImageButton();
            editImageButton.ID = "DisplayButton";
            editImageButton.BorderWidth = 0;
            editImageButton.CssClass = "styleIconButton";
            editImageButton.CommandName = "EditImageButton";
            editImageButton.EnableViewState = false;
            editImageButton.ToolTip = "Edit";
            editImageButton.Attributes["OnClick"] = JScriptGenerator.BuildFadePageScript();
            editImageButton.DataBinding += new EventHandler( EditImageButton_DataBinding );
            editImageButton.Command += ButtonClickHandler;
            container.Controls.Add( editImageButton );

            // Create spacer label
            Label spacerLabel = new Label();
            spacerLabel.Width = 5;
            spacerLabel.EnableViewState = false;
            container.Controls.Add( spacerLabel );

            // Create and configure the TrashImageButton Image button
            ImageButton trashCanImageButton = new ImageButton();
            trashCanImageButton.ID = "DeleteButton";
            trashCanImageButton.BorderWidth = 0;
            trashCanImageButton.CssClass = "styleIconButton";
            trashCanImageButton.CommandName = "TrashCanImageButton";
            trashCanImageButton.EnableViewState = false;
            trashCanImageButton.ToolTip = "Delete";
            trashCanImageButton.Attributes["OnClick"] = JScriptGenerator.BuildFadePageScript();
            trashCanImageButton.DataBinding += new EventHandler( TrashCanImageButton_DataBinding );
            trashCanImageButton.Command += ButtonClickHandler;
            container.Controls.Add( trashCanImageButton );


         private void EditImageButton_DataBinding( object sender, System.EventArgs e ) {

            // Use the sender to get a reference to the current DataGrid row
            ImageButton editImageButton = ( ImageButton )sender;
            DataGridItem currentGridRow = ( DataGridItem )editImageButton.NamingContainer;

            if( IsPopulated ) {
               // Set Highlighting script
               base.SetButtonHighlighting( editImageButton, currentGridRow.DataSetIndex );

               // Configure Icon button properties
               //CodeDcl codeDcl = ( CodeDcl )currentGridRow.DataItem;

               currentGridRow.Cells[0].Text = codeDcl.CodeValue.ToString();
               currentGridRow.Cells[1].Text = codeDcl.ShortDescription;
               currentGridRow.Cells[2].Text = codeDcl.LongDescription;

               if( _selectedCodeValue == codeDcl.CodeValue ) {

                  editImageButton.ImageUrl = editImageButton.Page.Request.ApplicationPath + "images/AsIconEditing.gif";
                  editImageButton.Attributes["OnClick"] = JScriptGenerator.BuildDisableControlScript();
               else {
                  editImageButton.CommandArgument = codeDcl.CodeValue;
                  editImageButton.ImageUrl = editImageButton.Page.Request.ApplicationPath + "images/AsIconEdit.gif";
                  editImageButton.Attributes["OnClick"] = JScriptGenerator.BuildShowConfirmScript();
            else {
               currentGridRow.Visible = false;


         private void TrashCanImageButton_DataBinding( object sender, System.EventArgs e ) {

            // Use the sender to get a reference to the current DataGrid row
            ImageButton trashCanImageButton = ( ImageButton )sender;
            DataGridItem currentGridRow = ( DataGridItem )trashCanImageButton.NamingContainer;

               //CodeDcl codeDcl = ( CodeDcl )currentGridRow.DataItem;
               trashCanImageButton.ImageUrl = trashCanImageButton.Page.Request.ApplicationPath + "images/AsIconTrash.gif";
               trashCanImageButton.CommandArgument = codeDcl.CodeValue;
               trashCanImageButton.Attributes["OnClick"] = JScriptGenerator.BuildShowConfirmScript();
               trashCanImageButton.Attributes["OnClick"] += JScriptGenerator.BuildShowConfirmMessageScript( "Are you sure?" );


Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If you mean DataGrid, versus GridView--the DataGrid control is not a good choice any longer outside of 1.1, so my suggestion would be to use the GridView instead.
yabhijitAuthor Commented:
I cant change it directly from Datagrid to GridView...and its a big change as well...and I need a fix for the same..Initially I receved an error on

On _DataBinding( object sender, System.EventArgs e )  event

CodeDcl codeDcl = ( CodeDcl )currentGridRow.DataItem;

as it was not pulling the data from

DataGridItem currentGridRow = ( DataGridItem )editImageButton.NamingContainer;

I tried by workaround(by passing latest datasource in constructor of ITemplate and pulls required data and access and set the data in databinf event, but later realise that I have to fix the above DataItem issue properly) but the the real culprit is above line where its not pulling the data using NamingContainer

Is there a way using by implementing IDataItemContainer and set the latest DataItem?? OR any other easy way to fix the same??
Bob LearnedCommented:
It has been waaaay too long for me and the DataGrid control, and I doubt you will find many people who can help you 12 years after the control was first created, which is probably why this question went unanswered for so long.  There isn't a lot of reference material on the DataGrid either, because it is all pointing to the GridView.
yabhijitAuthor Commented:
I have fixed the issue by adding below properties

 base.RequiresDataBinding = true;
base.DataSource = this.DataSource;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yabhijitAuthor Commented:
tried the approch and it worked
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.