This is a migrated thread and some comments may be shown as answers.

Bind data to new GridTemplateColumn

8 Answers 257 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 12 Jun 2014, 05:03 PM
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.

8 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 12 Jun 2014, 05:59 PM
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
0
RB
Top achievements
Rank 1
answered on 12 Jun 2014, 07:23 PM
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?  
0
RB
Top achievements
Rank 1
answered on 12 Jun 2014, 08:50 PM
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!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Jun 2014, 05:16 PM
Hello,

Your problem is resolved?

Thanks,
Jayesh Goyani
0
RB
Top achievements
Rank 1
answered on 17 Jun 2014, 05:17 PM
No. Not resolved yet!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Jun 2014, 07:04 PM
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
0
RB
Top achievements
Rank 1
answered on 19 Jun 2014, 05:40 PM
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!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Jun 2014, 06:17 PM
Hello,

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

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
RB
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
RB
Top achievements
Rank 1
Share this question
or