Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

RadGrid with asp:DropDownList not binding to correct value

Posted on 2011-03-09
2
Medium Priority
?
5,164 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

824 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