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

Access RadDropDownList in Grid EditItemTemplate to fill it with values from a data source

6 Answers 499 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Max
Top achievements
Rank 1
Max asked on 12 Jun 2014, 08:39 AM
Hello everybody,

I already searched for the solution of my problem but did not find anything to work. I Build a RadGrid an bind it to a SQL Datasource. Now I want to fill the Dropdownlists in the Radgrid with values from a different code behind stored procedure. Somehow I can not access the two DropDownLists from my code behind. I would be very thankful if someone could help me out. Here is my code:

<telerik:RadGrid runat="server" ID="gridFktWipoHague" Culture="de-DE">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="LAND_KZ" ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="PMMA_Add">
                <EditItemTemplate>
                    <telerik:RadDropDownList runat="server" ID="PMMA_Add_DropDown">
                    </telerik:RadDropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridDateTimeColumn DataField="HAGUE_DATUM">
            </telerik:GridDateTimeColumn>
            <telerik:GridTemplateColumn>
                <EditItemTemplate>
                    <telerik:RadDropDownList runat="server" ID="HAGUE_Add_DropDown">
                    </telerik:RadDropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="HAGUE_DELETE">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn ReadOnly="true" DataField="Counter_pm">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn ReadOnly="true" DataField="Counter_ps">
            </telerik:GridBoundColumn>
            <telerik:GridCheckBoxColumn DataField="TMVIEW">
            </telerik:GridCheckBoxColumn>
        </Columns>
    </MasterTableView>
    <ExportSettings>
        <Pdf PageWidth="">
        </Pdf>
    </ExportSettings>
 
</telerik:RadGrid>

Thank you very much,

Max

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Jun 2014, 09:04 AM
Hi Max,

Please try the following code snippet to access EditItemTemplate controls from code behind.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{   
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
   GridEditableItem editItem = (GridEditableItem)e.Item;
   //Access the RadDropDownList
   RadDropDownList rddlAdd = (RadDropDownList)editItem.FindControl("PMMA_Add_DropDown");
   rddlAdd.DataSource = //Set datasource
   //Assign the values to be displayed
   rddlAdd.DataTextField = "";
   rddlAdd.DataValueField = "";
  }
}

Thanks,
Princy

0
Max
Top achievements
Rank 1
answered on 12 Jun 2014, 09:31 AM
Thank you very much for the fast answer Princy!

Now I am able to access the control. Unfortunately, although I bind a DataView as Source, the DropDownList still is displayed as empty? 
Is there an exact time in the pages loading steps when I have to fire the ItemDataBound Event?

Thank you very much again!

Max
0
Max
Top achievements
Rank 1
answered on 12 Jun 2014, 11:15 AM
Dear Princy,

now I got yout solution working and switched to using a RadComboBox. All without Problem.
Now I implemented a loop to read Databank Entries and compare them with my combobox item values, when they are the same I check the box item. I want this for every combobox in every row seperately, unfortunately when the page is loaded all comboboxes in the GridView have the same items checked.

Again I would appreciate your help! Thank you very much!









0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Jun 2014, 05:27 AM
Hi Max,

I'm not clear about your requirement, I guess you wan to bind the ComboBox with the corresponding row values. Please try the following code snippet.

ASPX:
<telerik:GridTemplateColumn HeaderText="ShipCountry">
    <ItemTemplate>
        <telerik:RadComboBox ID="rcbCountry" runat="server">
        </telerik:RadComboBox>
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{   
  if (e.Item is GridDataItem)
  {
   GridDataItem dataItem = (GridDataItem)e.Item;
   RadComboBox rcbCountry = (RadComboBox)dataItem.FindControl("rcbCountry");
   //bind combobox with data
   //set the selected value
   rcbCountry.SelectedValue = DataBinder.Eval(dataItem.DataItem, "ShipCountry").ToString();
  }
}

Thanks,
Princy
0
Max
Top achievements
Rank 1
answered on 13 Jun 2014, 05:37 AM
Dear Princy,

thank you again for you answer!

What I want is to check different checkboxes and therefore entries in each row. For every row the checkbox entries are the same, only the state (checked/unchecked) of the row specific checkboxes should be differen. Here some code to make it a bit clearer:

foreach (DataRowView item in mainHagueSource)
{
    if (item["PMMA_Add"].ToString() != "")
    {
        string strAdd = item["PMMA_Add"].ToString();
        string[] strAddArray = strAdd.Split(',');
 
        foreach (RadComboBoxItem comboItem in comboAddPMMA.Items)
        {
            foreach (string addItem in strAddArray)
            {
                if (addItem == comboItem.Value.ToString())
                {
                    comboItem.Checked = true;
                    strPMMANumbers.Add(comboItem.Value.ToString());
                }
            }
        }
    }
}

Bye

Max
0
Max
Top achievements
Rank 1
answered on 13 Jun 2014, 07:42 AM
Hello again,

I got it solved. Bad Brainbug. As you sure see from the code I checked every row in the MainDatasource for every single Combobox.....
With ID comparison everthing works fine.

Thank you very much for your help!

best regards,

Max
Tags
Grid
Asked by
Max
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Max
Top achievements
Rank 1
Share this question
or