Bind data to new GridTemplateColumn

9 posts, 1 answers
  1. RB
    RB avatar
    126 posts
    Member since:
    Dec 2013

    Posted 12 Jun 2014 Link to this post

    I have a Radgrid with a text box template column:
    templateColumn = new GridTemplateColumn();
    templateColumnName = "BAN";
    this._RadGrid1.MasterTableView.Columns.Add(templateColumn);
    templateColumn.ItemTemplate = new TextBoxTemplate(templateColumnName);
    templateColumn.HeaderText = templateColumnName;
    templateColumn.DataField = this._PriceDealProductBanTable.BillingAccountNumberColumn.ColumnName;
    templateColumn.AllowFiltering = false;
    The TextBoxTemplateColumn is as follows:
    public class TextBoxTemplate : ITemplate
        {
            protected RadTextBox _textBox;      
            string _columnName;
            public TextBoxTemplate(string columnName)
            {
                this._columnName = columnName;
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
                
                this._textBox = new RadTextBox();
                this._textBox.ID = this._columnName;           
                container.Controls.Add(this._textBox);           
            }      
    }
    templateColumn.DataField = this._PriceDealProductBanTable.BillingAccountNumberColumn.ColumnName; doesnt do the trick of binding the textbox to a column. How  can I achieve it? I want the current values to be displayed on the text box.
  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 12 Jun 2014 in reply to RB Link to this post

    Hello,

    Please try with the below code snippet.

    public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
     
     
                GridTemplateColumn templateColumn = new GridTemplateColumn();
                string templateColumnName = "BAN";
                this.RadGrid1.MasterTableView.Columns.Add(templateColumn);
                templateColumn.ItemTemplate = new TextBoxTemplate(templateColumnName);
                templateColumn.HeaderText = templateColumnName;
                templateColumn.DataField = "Name";
                templateColumn.AllowFiltering = false;
     
     
            }
     
            protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Rows.Add("name1");
                dt.Rows.Add("name2");
                RadGrid1.DataSource = dt;
            }
        }
     
        public class TextBoxTemplate : ITemplate
        {
            protected RadTextBox _textBox;
            string _columnName;
            public TextBoxTemplate(string columnName)
            {
                this._columnName = columnName;
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
     
                this._textBox = new RadTextBox();
                this._textBox.ID = this._columnName;
                container.Controls.Add(this._textBox);
                _textBox.DataBinding += new EventHandler(_textBox_DataBinding);
            }
     
            void _textBox_DataBinding(object sender, EventArgs e)
            {
                RadTextBox txt = (RadTextBox)sender;
                GridDataItem container = (GridDataItem)txt.NamingContainer;
                txt.Text = ((DataRowView)container.DataItem)["Name"].ToString();
            }
        }


    http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. RB
    RB avatar
    126 posts
    Member since:
    Dec 2013

    Posted 12 Jun 2014 in reply to Jayesh Goyani Link to this post

    Thanks for the quick response. But when I edit the text and click on the insert button, the new value is inserted in the table, but it is not reflected in the grid.
    txt.Text = ((DataRowView)container.DataItem)["Name"].ToString();
    The above code still fetches the old value. How can I fetch the new value?  
  5. RB
    RB avatar
    126 posts
    Member since:
    Dec 2013

    Posted 12 Jun 2014 in reply to Jayesh Goyani Link to this post

    The RadGrid had a textbox, i can edit its values and on click of Insert button, the new value is inserted in the DB. But this new value is not reflected in the text box. Instead it shows the previous value!
  6. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 17 Jun 2014 in reply to RB Link to this post

    Hello,

    Your problem is resolved?

    Thanks,
    Jayesh Goyani
  7. RB
    RB avatar
    126 posts
    Member since:
    Dec 2013

    Posted 17 Jun 2014 in reply to Jayesh Goyani Link to this post

    No. Not resolved yet!
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 18 Jun 2014 in reply to RB Link to this post

    Hello,

    I have tried with the below code snippet but not able to reproduce this issue. I am able to get updated value in update event.

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            GridTemplateColumn templateColumn = new GridTemplateColumn();
            string templateColumnName = "BAN";
            this.RadGrid1.MasterTableView.Columns.Add(templateColumn);
            templateColumn.ItemTemplate = new TextBoxTemplate(templateColumnName);
            templateColumn.HeaderText = templateColumnName;
            templateColumn.DataField = "Name";
            templateColumn.UniqueName = "testColumn";
            templateColumn.AllowFiltering = false;
        }
     
     
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable dt = new DataTable();
            if (Session["TempData"] == null)
            {
     
                dt.Columns.Add("Name");
                dt.Rows.Add("name1");
                dt.Rows.Add("name2");
                Session["TempData"] = dt;
            }
     
            dt = (DataTable)Session["TempData"];
     
            RadGrid1.DataSource = dt;
        }
     
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            string strNewName = (item["testColumn"].FindControl("BAN") as RadTextBox).Text;
     
            DataTable dt = (DataTable)Session["TempData"];
            dt.Rows[0][0] = strNewName; //Temporary i have set this updated value in first row
            Session["TempData"] = dt;
        }
     
        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            string strNewName = (item["testColumn"].FindControl("BAN") as RadTextBox).Text;
            DataTable dt = (DataTable)Session["TempData"];
            dt.Rows.Add(strNewName);
            Session["TempData"] = dt;
        }
    }
     
    public class TextBoxTemplate : ITemplate
    {
        protected RadTextBox _textBox;
        string _columnName;
        public TextBoxTemplate(string columnName)
        {
            this._columnName = columnName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
     
            this._textBox = new RadTextBox();
            this._textBox.ID = this._columnName;
            container.Controls.Add(this._textBox);
            _textBox.DataBinding += new EventHandler(_textBox_DataBinding);
        }
     
        void _textBox_DataBinding(object sender, EventArgs e)
        {
            RadTextBox txt = (RadTextBox)sender;
            GridDataItem container = (GridDataItem)txt.NamingContainer;
            txt.Text = ((DataRowView)container.DataItem)["Name"].ToString();
        }
    }




    Thanks,
    Jayesh Goyani
  9. RB
    RB avatar
    126 posts
    Member since:
    Dec 2013

    Posted 19 Jun 2014 in reply to Jayesh Goyani Link to this post

    The NeedDataSource event was not being fired after Rebind is called. Hence I never got the updated values!So had to set Grid.DataSource to null, before calling the Rebind method. It worked!
  10. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 27 Jun 2014 in reply to RB Link to this post

    Hello,

    Can you please provide your code or create new project and add your code in it and send to us?

    Thanks,
    Jayesh Goyani
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017