Solved

RadGrid with asp:DropDownList not binding to correct value

Posted on 2011-03-09
2
5,067 Views
Last Modified: 2013-03-06
Hello,
I have a RadGrid with an asp:DropDownList.  When I bind the control to the datasource, all the text fields are populated correctly; however the drop-down lists are defaulted to the item at the top of the list instead of the value in the database.  How do I get it to select the correct value from the dropdownlist when it binds?
Thank you!
<telerik:RadGrid ID="MyGrid" runat="server"
            OnItemDataBound="MyGrid_ItemDataBound"
            OnUpdateCommand="MyGrid_UpdateCommand"
             OnInsertCommand="MyGrid_InsertCommand"
             OnDeleteCommand="MyGrid_DeleteCommand"
            OnNeedDataSource="MyGrid_NeedDataSource"
            OnItemDeleted ="MyGrid_ItemDeleted"
            AutoGenerateColumns="false"
            AllowAutomaticInserts="true"
             MasterTableView-CommandItemDisplay="Top"
           OnItemCreated="MyGrid_ItemCreated"
            
            >
         <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridEditCommandColumn EditText="Edit" InsertText="Add" ButtonType="LinkButton" CancelText="Cancel" UpdateText="Update" ></telerik:GridEditCommandColumn>
                 <telerik:GridBoundColumn DataField="TransportMode"  DataType="System.String" HeaderText="Transport Modes" HeaderStyle-Wrap="false" />
                     <telerik:GridButtonColumn ConfirmText="Delete this row?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                        UniqueName="DeleteColumn">
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                    </telerik:GridButtonColumn>

            </Columns>

            <EditFormSettings EditFormType="Template">
                <FormTemplate >
                    <table border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td align="char">Transport Modes: </td>
                    
                 <asp:DropDownList ID="ddlTransportMode"  runat="server" SelectedValue='<%# Bind("TransportationModeID") %>' AppendDataBoundItems="true" DataTextField="TransportMode" DataValueField="ID" AutoPostBack="true" OnSelectedIndexChanged="ddlTransportMode_SelectedIndexChanged"   /></td>
                      <td align="char">Select Owner: </td>
                   
                            <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                            </asp:Button>&nbsp;
                            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                        </td>
                    </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>
            
        </MasterTableView>
       </telerik:RadGrid>

Open in new window

protected void GridSpecialTemp_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        SetSpecialShippingTemp();
    }

    public void DataBindForChildControl(int theID, int prodID)
    {
        SetSpecialShippingTemp();
        MyGrid.DataBind();
       
    }
private void SetSpecialShippingTemp()
    {
MyGrid.DataSource = GetDataTable(sqlStringSpecial);
MyGrid.DataBind(); 
}

 protected void GridSpecialTemp_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {

        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {


            //populate transport modes
            DropDownList list = e.Item.FindControl("ddlTransportMode") as DropDownList;

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[strNameOfConnectionString].ConnectionString))
            {
                using (SqlCommand command = conn.CreateCommand())
                {
                    command.CommandText = "SELECT ID, TransportMode FROM MyTable order by TransportMode";
                    conn.Open();
                    list.DataSource = command.ExecuteReader();
                    list.DataBind();

                }
            }
	}
}

Open in new window

0
Comment
Question by:anixon93
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
anixon93 earned 0 total points
ID: 35086101
My co-worker found a solution.

I added the following code to GridSpecialTemp_ItemDataBound

 GridEditableItem editForm = (GridEditableItem)e.Item;
            (editForm.FindControl("ddlTransportMode") as DropDownList).SelectedValue = ((System.Data.DataRowView)(editForm.DataItem)).Row.ItemArray[1].ToString();

The index of ItemArray should be wherever the field is in your record.
0
 

Expert Comment

by:vpit
ID: 38959302
I have the same problem, but am only using a GridView and trying to change 1 column to a dropdown. I am currently getting a return value in the dropdown of "System.Data.DataRowView" and not the actual values from my database table. Any ideas why?

Thanks
Alex
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The principā€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question