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

[Solved] Programmatically Create & Bind Data to RadNumericTextBox In RadGrid

2 Answers 358 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 01 Jul 2009, 01:34 PM
I have a very simple table with 3 columns; ID (hidden), Quantity, Amount. The point of the grid is to allow users to add a new row, add values and the keeping adding rows with values. 

I am programatically creating the grid columns specifying the EditItemTemplate of the columns using a class that implements ITemplate as follows:

 
templateColumn = new GridTemplateColumn(); 
templateColumn.EditItemTemplate = new RadNumericTextBoxTemplate("txtQuantity""Quantity"); 
templateColumn.UniqueName = templateColumn.HeaderText = "Quantity"
templateColumn.DataField = "Quantity"
myGrid.MasterTableView.Columns.Add(templateColumn); 

public class RadNumericTextBoxTemplate : TemplateControlDefaults 
    protected RadNumericTextBox textBox; 
    string controlname; 
    string bindingName; 
 
    public RadNumericTextBoxTemplate(string ControlName, string BindingName) 
    { 
        this.controlname = ControlName; 
         
        if (BindingName != string.Empty) 
        { 
            this.bindingName = BindingName; 
        } 
    } 
 
    public override void InstantiateIn(System.Web.UI.Control container) 
    { 
        textBox = new RadNumericTextBox(); 
        textBox.ID = this.controlname; 
        textBox.Skin = defaultTextBoxSkinName; 
        textBox.Width = Unit.Pixel(100); 
 
        container.Controls.Add(textBox); 
        } 
    } 

The rows always need to be in Edit mode, InlIne editing in this case:

protected void myGrid_ItemDataBound(object sender, GridItemEventArgs e)  
{  
    if (e.Item is GridDataItem)  
    {  
        e.Item.Edit = true;  
    }  

How do I make sure that the items are bound to the text boxes? The data source is an IList<myObject> where myObject has the properties to match the columns (ID, Quantity, Amount).

Any help appreciated.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Jul 2009, 05:22 AM
Hi,

You need to bind the textbox to the Quantity field  using the DataBinding event as shown below.:

public override void InstantiateIn(System.Web.UI.Control container)  
    {  
        textBox = new RadNumericTextBox();  
        textBox.ID = this.controlname;  
        textBox.Skin = defaultTextBoxSkinName;  
        textBox.Width = Unit.Pixel(100);  
        textBox.DataBinding += new EventHandler(textBox_DataBinding); 
        container.Controls.Add(textBox);  
        }  
    } 
 
 public void textBox_DataBinding(object sender, EventArgs e) 
  { 
     RadNumericTextBox textBox = (RadNumericTextBox)sender; 
     GridEditableItem container = (GridEditableItem)textBox.NamingContainer; 
     textBox.Text = ((DataRowView)container.DataItem)["Quantity"].ToString() 
  } 


Thanks,
Princy
0
Carl
Top achievements
Rank 1
answered on 03 Jul 2009, 02:00 PM
That pretty much worked.

Thank you.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Carl
Top achievements
Rank 1
Share this question
or