Solved

RadGrid with asp:DropDownList not binding to correct value

Posted on 2011-03-09
2
5,036 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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