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

Can't find dynamically created text box control

2 Answers 78 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 02 Feb 2009, 09:14 AM
Hi,

I am creating pivot style grid. I can bind the data with row and column as well as dynamic text box control for each cell.
Rate field is text box and user can enter the data. I can create and bind the grid as follow. But when I read the data from each cells I got null referennce exception. Please let me know how do i get the data from control that dynamically created.

E.g
        

 

SGD

HKD

USD

2.0

2.5

AUD

1.0

1.5



public DataTable PivotTable()  
        {  
            DataTable dest = new DataTable("Pivoted");  
            dest.Columns.Add(" ");  
 
            CurrencyRate currencyRate = new CurrencyRate();  
            IList<DCCPP.BusinessEntities.Currency> bCurrencyList = RetrieveFXRateLocalBaseCurrency(currencyRate);  
            foreach (DCCPP.BusinessEntities.Currency bCurrency in bCurrencyList)  
            {  
                dest.Columns.Add(bCurrency.Code);  
            }  
 
            IList<DCCPP.BusinessEntities.Currency> sCurrencyList = RetrieveFXRateSupportedCurrency(currencyRate);  
            for (int i = 0; i < sCurrencyList.Count; i++)  
            {  
                dest.Rows.Add(dest.NewRow());  
            }  
            for (int r = 0; r < dest.Rows.Count; r++)  
            {  
                for (int c = 0; c < dest.Columns.Count; c++)  
                {  
                    if (c == 0)  
                        dest.Rows[r][0] = sCurrencyList[r].Code;  
                    //else  
                        //dest.Rows[r][c] = source.Rows[c - 1][r + 1];  
                }  
            }  
            dest.AcceptChanges();  
            return dest;  
        }  
 
        protected void rgFXRateDetail_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
 
            rgFXRateDetail.DataSource = PivotTable();  
        }  
 
        protected void rgFXRateDetail_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                (e.Item as GridDataItem)[(rgFXRateDetail.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].Font.Bold = true;  
                (e.Item as GridDataItem)[(rgFXRateDetail.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BackColor = System.Drawing.SystemColors.Control;  
                (e.Item as GridDataItem)[(rgFXRateDetail.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).UniqueName].BorderColor = System.Drawing.Color.White;  
 
 
                CurrencyRate currencyRate = new CurrencyRate();  
                IList<DCCPP.BusinessEntities.Currency> bCurrencyList = RetrieveFXRateLocalBaseCurrency(currencyRate);  
                //IList<DCCPP.BusinessEntities.Currency> sCurrencyList = RetrieveFXRateSupportedCurrency(currencyRate);  
                int j = 2;  
                for (int i = 1; i <= bCurrencyList.Count; i++)  
                {  
                    DCCPP.BusinessEntities.Currency currency = bCurrencyList[i - 1];  
                    //string str = e.Item.Cells[j].ID;  
 
                    string controlSuffix = currency.ID.ToString();  
 
                    TextBox txtRate = new TextBox();  
                    txtRate.ID = "txtRate" + controlSuffix;  
                      
                      
                    j++;  
 
                    e.Item.Cells[j].Controls.Add(txtRate);  
                      
                      
                }  
            }  
 
              
        }  
 
        protected void rgFXRateDetail_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            foreach (GridItem item in rgFXRateDetail.MasterTableView.Items)  
            {  
                if (item is GridDataItem)  
                {  
                    GridDataItem current = item as GridDataItem;  
                      
                    CurrencyRate currencyRate = new CurrencyRate();  
                    IList<DCCPP.BusinessEntities.Currency> bCurrencyList = RetrieveFXRateLocalBaseCurrency(currencyRate);  
                    //IList<DCCPP.BusinessEntities.Currency> sCurrencyList = RetrieveFXRateSupportedCurrency(currencyRate);  
                    int j = 2;  
                    for (int i = 1; i <= bCurrencyList.Count; i++)  
                    {  
                        DCCPP.BusinessEntities.Currency currency = bCurrencyList[i - 1];  
                        string controlSuffix = currency.ID.ToString();  
                        j++;  
                        string fxRate = ((TextBox)current.Cells[j].FindControl("txtRate" + controlSuffix)).Text;  
     
                    }  
                      
 
                }  
 
            }  
        } 

2 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 1
answered on 04 Feb 2009, 04:00 AM
It is working now. Pls don't bother.

Thanks ,
Alex
0
Tsvetoslav
Telerik team
answered on 04 Feb 2009, 08:28 AM
Hello Alex,

Try adding your TextBox creation code (in the ItemDataBound event) also to the ItemCreated event. Keep in mind that the ItemDataBound event is not always fired when ViewState is enabled for the grid. For more information you can review the following documentation: Commands that invoke Rebind() implicitly, Differences between ItemCreated and ItemDataBound events, Event Sequence.

Regards,
Tsvetoslav
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Alex
Top achievements
Rank 1
Answers by
Alex
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or