This is a migrated thread and some comments may be shown as answers.

Edit mode not setting dropDownvalues

3 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 07 May 2014, 02:17 PM
I have done this repeadedly in other grid but for some reason when I transformed to C# nothing seems to work for me when I try to translate VB ways to C# ways, The code is differenet but the functionality of grid stays the same.  I am trying to populate the Dropwdown list from a database which is successful, however i want to assign and id from the database to the drowpdownlist and its not working it will not assign at all.  Works fine in Vb, but C# no go.


<EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <table style="width:100%;background-color:antiquewhite">
                        <tr>
                            <td style="height:10px"></td>
                        </tr>
                        <tr>
                            <td style="width:10%;text-align:right">Name: </td>
                            <td style="width:90%;text-align:left"><asp:Label ID="lblName" runat="server" Text='<%#Bind("TokenFor") %>'></asp:Label></td>
                        </tr>
                        <tr>
                            <td style="height:3px"></td>
                        </tr>
                          <tr>
                            <td style="width:10%;text-align:right">Persona: </td>
                            <td style="width:90%;text-align:left"><asp:DropDownList ID="ddlPersona" runat="server"></asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="height:3px"></td>
                        </tr>
                        <tr>
                            <td style="width:10%;text-align:right">Email: </td>
                            <td style="width:90%;text-align:left"><asp:TextBox ID="txtEmail" runat="server" Text='<%#Bind("strTokenEmail") %>' Width="260px"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td style="height:3px"></td>
                        </tr>
                        <tr>
                            <td style="width:10%;text-align:right">Phone: </td>
                            <td style="width:90%;text-align:left"><asp:TextBox ID="txtPhone" runat="server" Text='<%#Bind("strPhoneNum") %>'></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td style="height:20px"></td>
                        </tr>
                         <tr>
                            <td style="height:3px"></td>
                        </tr>
                        <tr>
                            <td style="width:10%;text-align:right">Location: </td>
                            <td style="width:90%;text-align:left"><asp:DropDownList ID="ddlLocation" runat="server"></asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="height:3px"></td>
                        </tr>
                        <tr>
                            <td style="width:10%;text-align:right"></td>
                            <td style="width:90%;text-align:left">
                                 <asp:LinkButton ID="lnkSubmit" runat="server" Text='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "Insert" : "Update" %>'
                                   CommandName='<%# ((bool)DataBinder.Eval(Container, "OwnerTableView.IsItemInserted")) ? "PerformInsert" : "Update" %>'>
                                 </asp:LinkButton>
                                       
                                  <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </td>
                        </tr>
                        <tr>
                            <td style="height:10px"></td>
                        </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>
 
 
 
 protected void myRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            DropDownList ddlLocation = (DropDownList)item.FindControl("ddlLocation");
            DropDownList ddlPersona = (DropDownList)item.FindControl("ddlPersona");
 
            sql = "Select intPersonaId, strPersonaLtr + ' - ' + strPersona Persona from tblPersona where bitActive = 1";
            ddlPersona.DataSource = c.GetReader(sql);
            ddlPersona.DataTextField = "Persona";
            ddlPersona.DataValueField = "intPersonaId";
            ddlPersona.DataBind();
            ddlPersona.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "intPersonaId").ToString();
 
            sql = "Select intLocationId, strLocation from tblLocations where bitActive = 1";
            ddlLocation.DataSource = c.GetReader(sql);
            ddlLocation.DataTextField = "strLocation";
            ddlLocation.DataValueField = "intLocationId";
            ddlLocation.Items.Add(new ListItem("Pick Location", "0"));
            ddlLocation.DataBind();
            ddlLocation.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "intLocationTokenId").ToString();
        }





















3 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 07 May 2014, 02:18 PM
I forgot to mention that these values exist in my NeedDataSource of the radgrid.
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 May 2014, 05:55 AM
Hi Kevin,

Please take a look at the sample code snippet which works fine at my end. Please try and let me know if any concern.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true"    AutoGenerateEditColumn="true" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID"/>           
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity"/>
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <asp:DropDownList ID="ddlShipCity" runat="server">
                </asp:DropDownList>
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
  RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        DropDownList ddlShipCity= (DropDownList)editItem.FindControl("ddlShipCity");
        string sql = "Select ShipCity, OrderID from Orders";
        ddlShipCity.DataSource = GetDataTable(sql);
        ddlShipCity.DataTextField = "ShipCity";
        ddlShipCity.DataValueField = "OrderID";
        ddlShipCity.DataBind();
        ddlShipCity.SelectedValue = DataBinder.Eval(editItem.DataItem, "OrderID").ToString();
    }
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}

Thanks,
Princy
0
Kevin
Top achievements
Rank 1
answered on 08 May 2014, 01:42 PM
Hi Princy,

Thanks for the help, couple things different from yours to mine, but its working with what you gave me.  Thanks
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or