Multicolumn Radcombox insert a blank item

5 posts, 0 answers
  1. Calsh
    Calsh avatar
    12 posts
    Member since:
    Dec 2012

    Posted 27 Oct 2013 Link to this post

    Hi

    Using a Multicolumn Combobox with OpenAccess and trying to insert a blank item - as need to
     have the option of not selecting any item from the list - tried various solutions/posts with no success - code for combobox as below

     

    <telerik:RadComboBox runat="server" ID="RadComboBox1" Height="190px" Width="100%"
     
        MarkFirstMatch="true" DataSourceID="OpenAccessLinqDataSource1" EnableLoadOnDemand="true"
     
        HighlightTemplatedItems="true" DataValueField="PartID" DataTextField="PartNumber"
     
        AppendDataBoundItems="True" SelectedValue='<%# Bind("PartID") %>' ItemsPerRequest="10"
     
        EnableVirtualScrolling="True" Filter="Contains" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
     
        AutoPostBack="True">
     
        <HeaderTemplate>
     
            <ul>
     
                <li class="col1">Part Number</li>
     
                <li class="col2">Part Description</li>
     
            </ul>
     
        </HeaderTemplate>
     
        <ItemTemplate>
     
            <ul>
     
                <li class="col1">
     
                    <%# DataBinder.Eval(Container.DataItem, "PartNumber")%>
     
                </li>
     
                <li class="col2">
     
                    <%# DataBinder.Eval(Container.DataItem, "PartDescription")%></li>
     
            </ul>
     
        </ItemTemplate>
     
    </telerik:RadComboBox>

    Tried online demo http://www.telerik.com/help/aspnet-ajax/combobox-insert-default-item-when-databinding.html 

     Thanks

     

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    Hi Calsh,

    Please have a look into the complete code to insert a blank Item in RadMultiColumComboBox.

    ASPX:
    <telerik:RadComboBox runat="server" ID="RadComboBox1" Height="190px" Width="420px"
        AppendDataBoundItems="true" AutoPostBack="true" NoWrap="true">
        <HeaderTemplate>
            <ul>
                <li class="col1">City Name</li>
                <li class="col2">Country Name</li>
            </ul>
        </HeaderTemplate>
        <ItemTemplate>
            <ul>
                <li class="col1">
                    <%# Container.DataItem != null ? DataBinder.Eval(Container.DataItem, "Cityname") : DataBinder.Eval(Container, "Text")%>
                </li>
                <li class="col2">
                    <%# Container.DataItem != null ? DataBinder.Eval(Container.DataItem, "Cityname") : DataBinder.Eval(Container, "Text")%>
                </li>
            </ul>
        </ItemTemplate>
        <Items>
            <telerik:RadComboBoxItem Text="" />
        </Items>
    </telerik:RadComboBox>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        RadComboBox1.DataBind();
        String connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstring);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("SELECT [Cityname], [Countryname] FROM [City]", conn);
        DataTable data = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(data);
        }
        finally
        {
            conn.Close();
        }
        RadComboBox1.DataSource = data;
        RadComboBox1.DataTextField = "Cityname";
        RadComboBox1.DataValueField = "Countryname";
        RadComboBox1.DataBind();
    }

    Hope this will helps you.
    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Calsh
    Calsh avatar
    12 posts
    Member since:
    Dec 2012

    Posted 28 Oct 2013 Link to this post

    I'm using VB - changed as below - which puts a blank item in the combobox - but get error '   is not a valid value for Int32'  when 'update' record - the field being updated is int32 and allows nulls and acts as a lookup to another table

    (Using OpenAccess with simple binding and edit form template to allow editing of record)


    <li class="col1">
     
        <%# If(Container.DataItem IsNot Nothing, DataBinder.Eval(Container.DataItem, "PartNumber"), DataBinder.Eval(Container, "Text"))%>    
     
    </li>


    Thanks




  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    Hi Calsh,

    I guess your RadComboBox is inside another control.(Here for example inside RadGrid). I have tried a sample scenario and was able to update the data. Please have a look into the following code.
    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowAutomaticUpdates="true">
        <MasterTableView DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="EmployeeID">
            <Columns>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" FilterControlAltText="Filter OrderID column"
                    HeaderText="EmployeeID" SortExpression="EmployeeID" UniqueName="EmployeeID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ReportsTo" FilterControlAltText="Filter CustomerID column"
                    HeaderText="ReportsTo" SortExpression="ReportsTo" UniqueName="ReportsTo">
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <telerik:RadComboBox runat="server" ID="RadComboBox1" Height="190px" Width="420px"
                        AppendDataBoundItems="true" AutoPostBack="true" NoWrap="true" DataSourceID="SqlDataSource1"
                        DataTextField="EmployeeID" DataValueField="EmployeeID">
                        <HeaderTemplate>
                            <ul>
                                <li class="col1">EmployeeID</li>
                                <li class="col2">ReportsTo</li>
                            </ul>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <ul>
                                <li class="col1">
                                    <%# Container.DataItem != null ? DataBinder.Eval(Container.DataItem, "EmployeeID") : DataBinder.Eval(Container, "Text")%>
                                </li>
                                <li class="col2">
                                    <%# Container.DataItem != null ? DataBinder.Eval(Container.DataItem, "ReportsTo") : DataBinder.Eval(Container, "Text")%>
                                </li>
                            </ul>
                        </ItemTemplate>
                        <Items>
                            <telerik:RadComboBoxItem Text="" />
                        </Items>
                    </telerik:RadComboBox>
                    <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update"></asp:Button>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT top 5 [EmployeeID], [LastName],[ReportsTo] FROM [EmployeeDetails]"
        UpdateCommand="UPDATE [EmployeeDetails] SET [ReportsTo] = @ReportsTo WHERE [EmployeeID] = @EmployeeID">
        <UpdateParameters>
            <asp:Parameter Name="ReportsTo" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="EmployeeID" Type="Int32"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>

    Please make sure that your updating column allows null value to add into database table. For more help, please provide your complete code.

    Thanks,
    Shinu.
     
  6. Calsh
    Calsh avatar
    12 posts
    Member since:
    Dec 2012

    Posted 29 Oct 2013 Link to this post

    Tried as you suggested - but still no luck - the database field that is being updated allows nulls

    I'm using a GridDropDownColumn that is bound to a different data source than the RadGrid - got a solution by adding a blank item in the RadGrid_ItemDataBound Event as code below (removed other columns etc. to make it easier to read) - used this code to add a blank item to other dropdown lists (copied it but can't remember where found it) - the only disadvantage to this method is that the blank item appears at the bottom of the list - but otherwise works OK


    <telerik:RadGrid runat="server" DataSourceID="OpenAccessLinqDataSourceItems"
        AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
        AllowAutomaticUpdates="True" ID="RadGridItems" AutoGenerateColumns="False">
     
        <MasterTableView DataKeyNames="ItemID" DataSourceID="OpenAccessLinqDataSourceItems" EditMode="PopUp">
            <Columns>
                <telerik:GridDropDownColumn FilterControlAltText="Filter Part column" UniqueName="PartID"
                    DataSourceID="OpenAccessLinqDataSourceParts" DataField="PartID" HeaderText="Part Number"
                    SortExpression="PartID" ListValueField="PartID" AllowSorting="true" ListTextField="PartNumber"
                    DropDownControlType="DropDownList">
                </telerik:GridDropDownColumn>           
            </Columns>
     
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <telerik:RadComboBox runat="server" ID="RadComboBox1"
                DataSourceID="OpenAccessLinqDataSourceParts" EnableLoadOnDemand="true"
                                DataValueField="PartID" DataTextField="PartNumber"
                                AppendDataBoundItems="True" SelectedValue='<%# Bind("PartID") %>'
                                AutoPostBack="True" >
                            <HeaderTemplate>
                                <ul>
                                        <li class="col1">Part Number</li>
                                            <li class="col2">Part Description</li>
                                    </ul>
                            </HeaderTemplate>
                                <ItemTemplate>
                                    <ul>
                                                <li class="col1">
                                                    <%# DataBinder.Eval(Container.DataItem, "PartNumber")%>
                                                </li>
                                                <li class="col2">
                                                        <%# DataBinder.Eval(Container.DataItem, "PartDescription")%>
                                                </li>
                                        </ul>
                                </ItemTemplate>
                        </telerik:RadComboBox>
        </FormTemplate>
            </EditFormSettings>
     
        </MasterTableView>
    </telerik:RadGrid>
     
     
     
    Private Sub RadGridItems_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGridItems.ItemDataBound
     
     
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
            Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
     
            comboBoxPartID = (TryCast(editItem.FindControl("RadComboBox1"), RadComboBox))
            selectedPartIDValue = comboBoxPartID.SelectedValue
            With comboBoxPartID
                .Items.Add(New RadComboBoxItem("", "0"))
     
                If selectedPartIDValue = " " Then
                    .SelectedValue = CStr(0)
                Else
                    .SelectedValue = selectedPartIDValue
                End If
            End With
     
        End If
     
    End Sub





Back to Top
UI for ASP.NET Ajax is Ready for VS 2017