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

Dropdownlist control not find in Dynamic RadGrid

4 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohan
Top achievements
Rank 1
Mohan asked on 30 Jun 2012, 01:15 PM
Team,

In my scenario, I have an autogenerated Radgrid control to bind two Bound columns and remaining columns are Dropdown list. The bound and dropdown list control is binded in Grid. But when I try to get the value from the Dropdown list, I can't able to get the control Id (even i used FindControl).

IN ASPX
----------
 <telerik:RadGrid ID="dgPassanger" runat="server" AllowSorting="true" Visible="true"
                                            PageSize="5" AllowPaging="true" AllowFilteringByColumn="true" PagerStyle-AlwaysVisible="true"
                                            OnItemDataBound="Passenger_ItemDataBound" Width="715px">
                                            <MasterTableView CommandItemDisplay="None" AllowFilteringByColumn="false" AutoGenerateColumns="true">
                                                <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" />
                                            </MasterTableView>
                                            <ClientSettings>
                                                <Scrolling AllowScroll="true" />
                                                <Selecting AllowRowSelect="true" />
                                            </ClientSettings>
                                        </telerik:RadGrid>

IN ASPX.CS
---------------
        protected void PreparePaxGrid(List<Passenger> PaxList)
        {
            DataTable pivotedList = new DataTable();
            try
            {
                if (Session["PostflightTrip"] != null)
                {
                    TripLog = (PostflightTrip)Session["PostflightTrip"];

                    // Adding Dynamic Columns
                    GridBoundColumn boundColumn;
                    GridTemplateColumn templateColumn;
 
                    boundColumn = new GridBoundColumn();
                    boundColumn.DataField = "PaxCode";
                    boundColumn.HeaderText = "Pax Code";
                    dgPassanger.MasterTableView.Columns.Add(boundColumn);

                    boundColumn = new GridBoundColumn();
                    boundColumn.DataField = "PaxName";
                    boundColumn.HeaderText = "Pax Name";
                    dgPassanger.MasterTableView.Columns.Add(boundColumn);

                    foreach (var item in TripLog.TripLegs)
                    {

                        templateColumn = new GridTemplateColumn();
                        templateColumn.HeaderText = item.POLegsDescription;
                        templateColumn.ItemTemplate = new GridTemplate(item.LegID); // Bind Dropdown List
                        templateColumn.UniqueName = "FlightPurpose";

                        dgPassanger.MasterTableView.Columns.Add(templateColumn);
                    }

                    // Bind List into Grid
                    dgPassanger.DataSource = PaxLegList;
                    dgPassanger.DataBind();

                }
            }
            catch (Exception ex) { }
        }

        private class GridTemplate : ITemplate
        {
            protected DropDownList ddList;
            private int _legId = 0;

            public GridTemplate(int legId)
            {
                _legId = legId;
            }

            public void InstantiateIn(System.Web.UI.Control container)
            {
                ddList = new DropDownList();
                ddList.ID = "ddlFP" +_legId.ToString();
                ddList.DataBinding += new EventHandler(ddList_DataBinding);
                container.Controls.Add(ddList);
            }

            protected void ddList_DataBinding(object sender, EventArgs e)
            {
                DropDownList dl = (DropDownList)sender;
                GridDataItem container = (GridDataItem)dl.NamingContainer;
                dl.Items.Add(new ListItem("1","1"));
                dl.Items.Add(new ListItem("2", "2"));
                //dl.AutoPostBack = true;
                //dl.SelectedIndexChanged += new EventHandler(DisplayDDList_IndexChanged);
            }
        }

For Retrieving Value from Dropdown, I used the below code
 foreach (GridDataItem Item in grid.MasterTableView.Items)
                {

                    DropDownList ddlFlightPurpose = (DropDownList)Item["FlightPurpose"].FindControl("ddlFP1");

}

ISSUE: Every time it shows the NULL value.

Please resolve as soon as possible.

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jun 2012, 02:47 PM
Hello,

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
</telerik:RadAjaxManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<telerik:RadGrid ID="dgPassanger" runat="server" AllowSorting="true" Visible="true" AutoGenerateColumns="false"
    PageSize="5" AllowPaging="true" AllowFilteringByColumn="true" PagerStyle-AlwaysVisible="true"
     Width="715px" onitemdatabound="dgPassanger_ItemDataBound">
    <MasterTableView CommandItemDisplay="None" AllowFilteringByColumn="false" AutoGenerateColumns="true">
        <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" />
    </MasterTableView>
    <ClientSettings>
        <Scrolling AllowScroll="true" />
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
</telerik:RadGrid>
<br />
<asp:Button ID="Button1" runat="server" Text="Get the dropdown column" onclick="Button1_Click" />


  protected void Page_Init(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(Int32));
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add(1, "Name1");
            dt.Rows.Add(2, "Name2");
 
            dynamic data = new[] {
              new { ID = 1, Name ="name1"},
              new { ID = 2, Name = "name2"},
              new { ID = 3, Name = "name3"},
              new { ID = 4, Name = "Name4"},
               new { ID = 5, Name ="name5"}
            };
 
            PreparePaxGrid(data, dt);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }
 
        protected void dgPassanger_ItemDataBound(object sender, GridItemEventArgs e)
        {
 
        }
 
        protected void PreparePaxGrid(dynamic data, DataTable dt)
        {
 
            try
            {
 
                // Adding Dynamic Columns
                GridBoundColumn boundColumn;
                GridTemplateColumn templateColumn;
 
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "ID";
                boundColumn.HeaderText = "ID from dyanamic";
                dgPassanger.MasterTableView.Columns.Add(boundColumn);
 
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "Name";
                boundColumn.HeaderText = "Name from dyanamic";
                dgPassanger.MasterTableView.Columns.Add(boundColumn);
 
 
                for (int i = 0; i < 2; i++)
                {
 
                    templateColumn = new GridTemplateColumn();
                    templateColumn.HeaderText = "ID " + i.ToString();
                    templateColumn.ItemTemplate = new GridTemplate(i); // Bind Dropdown List
                    templateColumn.UniqueName = "FlightPurpose" + i;
 
                    dgPassanger.MasterTableView.Columns.Add(templateColumn);
                }
                dgPassanger.MasterTableView.AutoGenerateColumns = false;
                dgPassanger.DataSource = data;
                dgPassanger.DataBind();
 
 
            }
            catch (Exception ex)
            {
 
            }
        }
 
        private class GridTemplate : ITemplate
        {
            protected DropDownList ddList;
            private int _legId = 0;
 
            public GridTemplate(int legId)
            {
                _legId = legId;
            }
 
            public void InstantiateIn(System.Web.UI.Control container)
            {
                ddList = new DropDownList();
                ddList.ID = "ddlFP" + _legId.ToString();
                ddList.DataBinding += new EventHandler(ddList_DataBinding);
                container.Controls.Add(ddList);
            }
 
            protected void ddList_DataBinding(object sender, EventArgs e)
            {
                DropDownList dl = (DropDownList)sender;
                GridDataItem container = (GridDataItem)dl.NamingContainer;
                dl.Items.Add(new ListItem("1", "1"));
                dl.Items.Add(new ListItem("2", "2"));
                //dl.AutoPostBack = true;
                //dl.SelectedIndexChanged += new EventHandler(DisplayDDList_IndexChanged);
            }
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in dgPassanger.MasterTableView.Items)
            {
                for (int i = 0; i < 2; i++)
                {
                    DropDownList ddl = item.FindControl("ddlFP" + i.ToString()) as DropDownList;
// access your DropDownList here
                }
            }
        }


Thanks,
Jayesh Goyani
0
Mohan
Top achievements
Rank 1
answered on 30 Jun 2012, 08:36 PM
Jayesh,

Thanks for your reply, I am still can't able to retrieve the value from Drop down list, Please check the screen shot.

Addl info, when I check the view source, it's look like below.

<div id="ctl00_ctl00_MainContent_PostFlightBodyContent_ctl00_ctl00_MainContent_PostFlightBodyContent_dgPassangerPanel">
<div id="ctl00_ctl00_MainContent_PostFlightBodyContent_dgPassanger" class="RadGrid RadGrid_Office2010Silver" style="width:715px;">
<input id="ctl00_ctl00_MainContent_PostFlightBodyContent_dgPassanger_ClientState" name="ctl00_ctl00_MainContent_PostFlightBodyContent_dgPassanger_ClientState" type="hidden" />
</div>





Thanks
Mohan
0
Mohan
Top achievements
Rank 1
answered on 01 Jul 2012, 03:09 PM
Dynamic Grid  binded in the Nested master page, in the page i used ajax settings like below:

                            <telerik:AjaxSetting AjaxControlID="dgPassanger">
                                <UpdatedControls>
                                    <telerik:AjaxUpdatedControl ControlID="dgPassanger" LoadingPanelID="RadAjaxLoadingPanel1" />
                                </UpdatedControls>
                            </telerik:AjaxSetting>
                            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                                <UpdatedControls>
                                    <telerik:AjaxUpdatedControl ControlID="dgPassanger" LoadingPanelID="RadAjaxLoadingPanel1" />
                                </UpdatedControls>
                            </telerik:AjaxSetting>
0
Gajanan
Top achievements
Rank 2
answered on 17 Aug 2015, 10:30 AM
Hil,
My requirement is Bind the grid dynamic , and add the controls like Text Box And rad Como box at run time by Item value.
so far have done the control adding and now my grid is ready to take values from user , 
but when user selected value from rad combo box (dynamic added control in grid), and click the save button i cannot find the Rad Combo box 
pls suggest me the way how to get the values for dynamically added controls in Rad-grid on button click.
below are the steps i followed
1. Added Grid in Aspx. with Autogenerated Column = true
2. On Item created event added dynamic controls in Grid. 
3. bind the data to ad combo box (dynamic added control in grid) 
4. on save Button i have to save the selected values from rad combo box (dynamic added control in grid), to data base
in above step 1 to 3 are done , but step 4 making me trouble .

below is code for button click.
protected void btnSaveAll_Click(object sender, EventArgs e)
{
    Questionnaire MyQue = Questionnaire.getQuestionnaire(mQuestionnaireid); 
    ArrayList ColumnLst = GetColumnList();
                Dictionary<string,string> RowSplitValue = new Dictionary<string,string>();
                foreach (GridEditableItem Item in rgPivotQuestionnaireResult.MasterTableView.Items)
                {
                    foreach (GridColumn Column in rgPivotQuestionnaireResult.MasterTableView.RenderColumns)
                    {
                        if (ColumnLst.Contains(Column.UniqueName))
                        {
                            //RowSplitValue = new Dictionary<string, string>(Item.SavedOldValues[Column.UniqueName]);
                            if (Item[Column].Controls.Count > 0)
                            {
                                Control ItemControle= new Control();
                                ItemControle = Item[Column].Controls[0];
                                if (ItemControle is TextBox)
                                {
                                    TextBox TxtBx = (TextBox)Item[Column].Controls[0];
                                }
                                if (Item[Column].Controls.Count > 1)
                                {
                                    ItemControle = Item[Column].Controls[1];
                                    if (ItemControle is RadComboBox)
                                    {
                                        RadComboBox RCB = (RadComboBox)Item[Column].Controls[1];
                                          MyQue .Answervalue = RCB.SelectedValue;
                                    }
                                }
                            }
                        }
                    }
                }
   MyQue .Save();
}
Tags
Grid
Asked by
Mohan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Mohan
Top achievements
Rank 1
Gajanan
Top achievements
Rank 2
Share this question
or