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

GridDropDownColumn question

1 Answer 51 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 31 Jan 2012, 03:08 PM
I have two tables, customers and addresses.  Each customer may have many addresses, with one default address. Simplifed a bit they contain the following fields:

Customers table - Name, CustomerID, DefaultAddressID
Addresses table - Address, AddressID, CustomerID

I am using a RadGrid to edit customers, and I would like to add a function to select the default address using a GridDropDownColumn. Currently, this is listing all the addresses, not just the ones for that customer.

How can I restrict the dropdown box to only list addresses for that customer?

Kind Regards, Richard Jonas

1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 02 Feb 2012, 05:31 PM
Hello Richard,

My advice is to use a GridTemplateColumn for this purpose. You can define an EditItemTemplate with a RadComboBox and populate it when the edit form for a specific record is bound:
<EditItemTemplate>
    <telerik:RadComboBox ID="RadComboBox1" runat="server">
    </telerik:RadComboBox>
</EditItemTemplate>

As you probably know the Northwind table has a similar relation between the Orders and Customers table - a single customer can have multiple orders. CustomerID is a column in both tables and here is how the requirement can be handled in this context:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        string key = (e.Item as GridEditableItem).GetDataKeyValue("CustomerID").ToString();
        RadComboBox combo = e.Item.FindControl("RadComboBox1") as RadComboBox;
        combo.DataSource = GetComboData("SELECT CustomerID, OrderID FROM [Orders] WHERE CustomerID='"+key+"'");
        combo.DataValueField = "OrderID";
        combo.DataTextField = "OrderID";
        combo.DataBind();
        combo.SelectedValue = key;
    }
}

I hope this helps.

Greetings,
Tsvetina
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
Richard
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or