Solved

RadGrid with asp:DropDownList not binding to correct value

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 59
Set cookies HttpOnly and Secure 4 54
Expando 4 34
dynamic menu in asp.net c# 11 29
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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