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

DefaultInsertValue for Dropdownlist

3 Answers 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Berry
Top achievements
Rank 1
Berry asked on 27 Oct 2012, 09:38 PM
hi,

I am using a RadGrid with Popup form for Inserting and Editing. I have a dropdown in my Insert/Edit for which needs to be set to a default value when the template is opened for Inserting and the value from the grid if the record is being edited.

The Edit link works fine, and I can select the value in the dropdown. However, when I am trying to open the Add New form, I am seeing a 

'ddlPriority has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value'

Can someone please tell me what should I do so that I can "bind" the value to the existing entry for the edit and use a blank value for Insert?


My grid:
 <telerik:GridBoundColumn UniqueName="Priority" HeaderText="Priority" DataField="Priority" DefaultInsertValue="">
</telerik:GridBoundColumn>

My FormTemplate:
 <asp:DropDownList ID = "ddlPriority" runat = "server" DataTextField = "List_Item" DataValueField = "List_Value" TabIndex="6" Width="200px" DataSource ="<%# dsPriority() %>" ></asp:DropDownList>

My Code behind:
Public Function dsPriority() As DataSet
        Dim dsDataset As New DataSet
        Dim datatblPriority As New DataTable
        datatblPriority.Columns.Add("List_Item")
        datatblPriority.Columns.Add("List_Value")

        Dim drRow1 As DataRow
        Dim drRow2 As DataRow
        Dim drRow3 As DataRow
        Dim drRow4 As DataRow
        Dim drRow5 As DataRow

  drRow1 = datatblPriority.NewRow
        drRow2 = datatblPriority.NewRow
        drRow3 = datatblPriority.NewRow
        drRow4 = datatblPriority.NewRow
        drRow5 = datatblPriority.NewRow

 drRow1(0) = "Select"
         drRow
1(1) = ""

drRow2.Item(0) = "Critical"
        drRow2.Item(1) = "Critical"

        drRow3.Item(0) = "High"
        drRow3.Item(1) = "High"

        drRow4.Item(0) = "Medium"
        drRow4.Item(1) = "Medium"

        drRow5.Item(0) = "Low"
        drRow5.Item(1) = "Low"

datatblPriority.Rows.Add(drRow1)
        datatblPriority.Rows.Add(drRow2)
        datatblPriority.Rows.Add(drRow3)
        datatblPriority.Rows.Add(drRow4)
        datatblPriority.Rows.Add(drRow5)
        
 dsDataset.Tables.Add(datatblPriority)

        Return dsDataset
    End Function

3 Answers, 1 is accepted

Sort by
0
Berry
Top achievements
Rank 1
answered on 27 Oct 2012, 09:41 PM
Sorry, here is my correct code for the Dropdownlist in the Edit/Insert template form:

 <asp:DropDownList ID = "ddlPriority" runat = "server" SelectedValue='<%# Bind("Priority") %>' DataTextField = "List_Item" DataValueField = "List_Value" TabIndex="6" Width="200px" DataSource ="<%# dsPriority %>" >
</asp:DropDownList>
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Oct 2012, 05:59 PM
Hello,

<asp:DropDownList ID = "ddlPriority" runat = "server" SelectedValue='<%# Eval("Priority") == null ? null : Eval("Priority") %>' DataTextField = "List_Item" DataValueField = "List_Value" TabIndex="6" Width="200px" DataSource ="<%# dsPriority %>" >
</asp:DropDownList>
 
//OR
 
<asp:DropDownList ID = "ddlPriority" runat = "server" SelectedValue='<%# Eval("Priority") == DBNull.Value ? null : Eval("Priority") %>' DataTextField = "List_Item" DataValueField = "List_Value" TabIndex="6" Width="200px" DataSource ="<%# dsPriority %>" >
</asp:DropDownList>

//OR

<MasterTableView  DataKeyNames="Priority" >

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
    
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = e.Item as GridEditableItem;
                DropDownList ddlPriority = e.Item.FindControl("ddlPriority") as DropDownList;
                if (ddlPriority != null)
                {
                    string strPriority =Convert.ToString(item.GetDataKeyValue("Priority"));
                    if (!string.IsNullOrEmpty(strPriority))
                    {
                        // By Value
                        if (ddlPriority.Items.FindByValue(strPriority) != null)
                        {
                            ddlPriority.Items.FindByValue(strPriority).Selected = true;
                        }
 
                        // OR
                        // by Text
                        if (ddlPriority.Items.FindByText(strPriority) != null)
                        {
                            ddlPriority.Items.FindByText(strPriority).Selected = true;
                        }
                    }
                }
}
}


Thanks,
Jayesh Goyani
0
Berry
Top achievements
Rank 1
answered on 28 Oct 2012, 06:07 PM
Thanks Jayesh. I was missing DataTextField = "List_Item" DataValueField = "List_Value" parameters in the dropdownlist tag. After I added that my code worked without any issues. DefaultInsertValue works just fine to handle null values.

Thanks for taking your time!
Tags
Grid
Asked by
Berry
Top achievements
Rank 1
Answers by
Berry
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or