Solved

RadGrid with asp:DropDownList not binding to correct value

Posted on 2011-03-09
2
4,996 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now