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

Radgrid Drop Down Column

10 Answers 651 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 14 Jan 2011, 07:14 PM
I may be wrong but it seems to me that the only way to populate a drop down column in a grid is with databinding. Is there a way to poopulate the items as follows:

combobox.items.add("First Item");
combobox.items.add("Second Item");
etc.

I don't need to populate from tables for each drop down column.

Regards, Lee

10 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 14 Jan 2011, 09:46 PM
Lee - from the standpoint of good program design, use an template
the Item Template should be just a label bound to the current value
the Edit Item Template should be the drop down
since the Edit Item Template isn't created until an edit command is issued for that row it can be a drop down with ListItems

do you need code to demonstrate?
0
Andy
Top achievements
Rank 1
answered on 14 Jan 2011, 10:03 PM
Hi Lee,

Here are the code if you want to populate data for Dropdown List in ItemTemplate

<telerik:GridTemplateColumn UniqueName="Uname" HeaderText="Header Text">
     <ItemTemplate>
          <asp:DropDownList ID="myDD" Font-Size="XX-Small" runat="server"
           </asp:DropDownList>
   </ItemTemplate>
 </telerik:GridTemplateColumn>

VB.NET
Protected Sub radGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles radGrid1.ItemCreated
        If TypeOf e.Item Is GridDataItem Then
                Dim myDD As DropDownList
                myDD = DirectCast(item("Uname").FindControl("myDD"), DropDownList)
                ' Then add Data
        End If
End Sub
     

Andy.
0
Lee
Top achievements
Rank 1
answered on 17 Jan 2011, 09:13 PM
Thanks Andy,

I have added the Drop Down and I am able to set the value of the Drop Down List in the ItemDataBound method. My last question is how do I save the new value back to the Table. I have been testing the the UpdateCommand method but have not had any luck so far.

Regards, Lee
0
Elliott
Top achievements
Rank 2
answered on 17 Jan 2011, 09:16 PM
is the grid correctly bound?
does it have the needsdatasource event instantiated?
before you go nutz check that first
0
Lee
Top achievements
Rank 1
answered on 17 Jan 2011, 09:18 PM
Thanks Marianne

I did what you suggested. The last question is how to save the selected value back to the table. I have been testing with the updatecommand method but have not been successful so far. Here's the current code:

<telerik:GridTemplateColumn FilterControlAltText="Filter Access column" HeaderText="Access" UniqueName="Access">
                    <ItemTemplate>     
                        <asp:Label CssClass="label" ID="lblContainer" runat="server" Text='<%# Eval("FormAccess") >'></asp:Label>     
                    </ItemTemplate>     
                    <EditItemTemplate>     
                        <asp:DropDownList CssClass="dropdownlist" ID="ddlContainer" runat="server"></asp:DropDownList>     
                    </EditItemTemplate
</telerik:GridTemplateColumn>

Here's the CS

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((e.Item is GridDataItem) && e.Item.IsInEditMode)
    {
        GridDataItem item = e.Item as GridDataItem;
        DropDownList myDD = item.FindControl("ddlContainer") as DropDownList;
        myDD.Items.Add("View");
        myDD.Items.Add("Modify");
        myDD.Items.Add("Denied");
        GridEditableItem editForm = (GridEditableItem)e.Item;
        myDD.SelectedValue = ((System.Data.DataRowView)(editForm.DataItem)).Row.ItemArray[3].ToString().Trim();
    }
}
protected void RadGrid2_UpdateCommand(object sender, GridCommandEventArgs e)
{
    // trying to update value here.....
}

Can you stear me in the right direction on the update?

Regards, Lee
0
Lee
Top achievements
Rank 1
answered on 17 Jan 2011, 10:37 PM
Hi Marianne,

The grid seems to be bound porperly. Everything is saving except the value for the DropDownList in the Template column. Should it save automatically

Regards, Lee
0
Elliott
Top achievements
Rank 2
answered on 17 Jan 2011, 10:44 PM
where is the FormAccess datafield for the lblContainer coming from?  It needs to be the same value as
e.Item.Row.ItemArray[3]
0
Lee
Top achievements
Rank 1
answered on 17 Jan 2011, 10:49 PM
The datafield for the lblcontainer and the e.Item.Row.ItemArray[3] are the same. The ddl is set properly, saving the changed value is the challenge.
0
Elliott
Top achievements
Rank 2
answered on 17 Jan 2011, 11:06 PM
try setting AutoPostBack to true
then try to trigger a server event on SelectedValueChanged, which will then update the database (access file?, XML file?) corresponding to that field

I realize the problem is that the drop down is not coming from any database
that means you will have to store the changed value somewhere on the screen
0
Lee
Top achievements
Rank 1
answered on 19 Jan 2011, 09:09 PM
OK, I have the entire solution based on what Marianne provided and another post in the forums. If someone else needs the entire solution here it is:

Create a form with a rad grid and a hidden field.
Edit the Update and Insert Query for the Datasource to use the hidden field as the parameter for the field you want the drop down list  to populate.

For the drop down list make the column a template column
<telerik:GridTemplateColumn FilterControlAltText="Filter DistType column" 
    HeaderText="Type" UniqueName="DistType">
            <ItemTemplate>
               <asp:Label id="Label1" runat="server">
                  <%# DataBinder.Eval(Container.DataItem, "DistributionType")%>
               </asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList id="DistTypeDDL" runat="server" />
            </EditItemTemplate>
</telerik:GridTemplateColumn>

It needs to contain an ItemTemplate and an EditTemplate as in the code snippet.

In the ItemDataBound, InsertCommand and UpdateCommand events for the RadGrid put the following code:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        // access/modify the edit item template settings here
        DropDownList list = item.FindControl("DistTypeDDL") as DropDownList;
        list.Items.Add("Repairs");
        list.Items.Add("Supplies");
        list.Items.Add("Equipment Sales");
        list.Items.Add("Freight");
        list.Items.Add("Tax");
        GridEditableItem editForm = (GridEditableItem)e.Item;
        string CurrentType;
        try
        {
            CurrentType = ((System.Data.DataRowView)(editForm.DataItem)).Row.ItemArray[4].ToString();
            if (CurrentType != null)
            {
                list.SelectedValue = CurrentType.Trim();
            }
        }
        catch
        {
        }
    }
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    DropDownList list = editedItem.FindControl("DistTypeDDL") as DropDownList;
    NewType.Value = list.SelectedValue;
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    DropDownList list = editedItem.FindControl("DistTypeDDL") as DropDownList;
    NewType.Value = list.SelectedValue;
}

Done.
Tags
Grid
Asked by
Lee
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Lee
Top achievements
Rank 1
Share this question
or