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

Extra items being bound to RadComboBox inside a RadGrid TemplateColumn

1 Answer 49 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Devon
Top achievements
Rank 1
Devon asked on 24 Jan 2012, 08:48 PM
I have a RadGrid that is bound in the code behind, on on ItemDataBound I'm doing binding to a RadComboBox that is within a TemplateColumn inside the grid:
 
<tel:RadGrid runat="server" ID="rgCounties" OnItemCommand="rgCounties_ItemCommand"
    AutoGenerateColumns="False" OnItemDataBound="rgCountes_ItemDataBound" AllowMultiRowEdit="True"
    CellSpacing="0" GridLines="None">
    <MasterTableView>
        <Columns>
            <tel:GridBoundColumn Display="false" DataField="ID" UniqueName="SectionCountyID"  />
            <tel:GridTemplateColumn HeaderText="County" UniqueName="CountyColumn">
                <InsertItemTemplate>
                    <tel:RadComboBox runat="server" ID="rgrcCounty" DataTextField="CountyName" DataValueField="ID" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <tel:RadComboBox runat="server" ID="rgrcCounty" DataTextField="CountyName"
                        DataValueField="ID" />
                </ItemTemplate>
            </tel:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</tel:RadGrid>

So they can edit the County Dropdown at will, and click a save button below.
The county dropdown is being populated from a database table, but only has entries that are not currently in the grid.

For instance, Suppose i have 5 Counties:
{Lancaster, York, Berks, Adams, Allegheny}
the Grid has 3 entries for the counties "York, Adams, Allegheny".  Therefore, each combo box should have the two that are not present anywhere in the grid (Lancaster, Berks), as well as one entry for that grid row

So the grid would look like this:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, Adams}
Row 3: Combo with options {Lancaster, Berks, Allegheny}

However, the results I'm getting are:
Row 1: Combo with options {Lancaster, Berks, York}
Row 2: Combo with options {Lancaster, Berks, York, Adams}
Row 3: Combo with options {Lancaster, Berks, York, Adams, Allegheny}

Even though on Each row I'm rebinding the combobox and adding the missing one.
protected void rgCountes_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        var ditem = e.Item as GridDataItem;
        var combo = ((RadComboBox)ditem["CountyColumn"].FindControl("rgrcCounty"));
        var sectionCountyID = int.Parse(ditem["SectionCountyID"].Text);
        var sectionCounty = Model.SectionCounties.Where(sc => sc.ID == sectionCountyID).FirstOrDefault();
 
        combo.Bind(Model.Counties, false);
        if (sectionCounty != null)
        {
            combo.AddOrSelectByValue(sectionCounty.CountyID.ToString(), sectionCounty.County.CountyName);
            rnt.Text = sectionCounty.Percentage.ToString();
        }
    }    
}
        public static bool AddOrSelectByValue(this RadComboBox combo, string value, string text)
        {
            bool tf = combo.SelectByValue(value);
            if (!tf)
            {
                combo.Items.Add(new RadComboBoxItem(text, value));
                combo.SelectByValue(value);
            }
            return tf;
        }


You can see I'm rebinding each combo and then adding a new value on a per-row basis. But it seems that on each row, thecombo.Items.Add() is being applied additively.

Any idea what I'm doing wrong or how to fix it?

thanks!

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 25 Jan 2012, 06:12 AM
Hi Devon,

Try clearing the combobox items before calling the BindModel method. Or at the end of the if statement in the ItemDataBound event set its DataSource to null.

Regards,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Devon
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or