Colorpicker & Binding

2 posts, 0 answers
  1. Andrew
    Andrew avatar
    14 posts
    Member since:
    Jun 2009

    Posted 15 Jul 2009 Link to this post

    I have a problem with RadColorPicker in a RadGrid.

    <telerik:Radgrid ID="RadGrid1" (...)> 
      <MasterTableView (...)> 
        <EditFormSettings EditformType="Template"
                <span>Couleur: </span> 
                <telerik:RadColorPicker ID="RadColorPicker1" runat="server" Preset="Standard" ShowIcon="true" Skin="Black" PaletteModes="All" SelectedColor='<%# HexStringToColor(myCStr(Bind("Couleur"))) %>'

    In my code behind :

        Function HexStringToColor(ByVal hex As StringAs System.Drawing.Color 
            hex = hex.Replace("#"""
            If hex.Length <> 6 Then 
                Throw New Exception(hex & " n'est pas une valeur de couleur hexadecimale valide."
            End If 
            Dim r, g, b As String 
            r = hex.Substring(0, 2) 
            g = hex.Substring(2, 2) 
            b = hex.Substring(4, 2) 
            Return System.Drawing.Color.FromArgb(HexStringToBase10Int(r), HexStringToBase10Int(g), HexStringToBase10Int(b)) 
        End Function 
        Function HexStringToBase10Int(ByVal hex As StringAs Integer 
            Dim base10value As Integer = 0 
                base10value = System.Convert.ToInt32(hex, 16) 
            Catch ex As Exception 
                base10value = 0 
            End Try 
            Return base10value 
        End Function 
        Function myCStr(ByVal test As ObjectAs String 
            If isdbnull(test) Then 
                Return ("#ffffff"
                Return CStr(test) 
            End If 
        End Function 

    When I do :
     SelectedColor='<%# HexStringToColor(myCStr(Eval("Couleur"))) %>'
    It's works for the display but when i edit the value, the database is not updated.

    How can I do?

    Thanks in advance

  2. Tsvetie
    Tsvetie avatar
    1517 posts

    Posted 20 Jul 2009 Link to this post

    Hello Andrew,
    You need to manually convert the value of the SelectedColor property of the RadColorPicker to a string and set this as value of your update Parameter in the ItemCommand handler for the RadGrid. You need to do this because the SelectedColor property is of type Color, whereas your field in the DB is most probably of type string.

    For example:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ColorPickerDBConnectionString %>" 
        SelectCommand="SELECT [ColorID], [Color] FROM [Colors]" UpdateCommand="UPDATE Colors SET Color = @Color WHERE (ColorID = @ColorID)"
            <asp:Parameter Name="Color" Type="String" /> 
            <asp:Parameter Name="ColorID" Type="Int32" /> 
    <telerik:RadGrid ID="RadGrid1" runat="server" PageSize="20" AutoGenerateColumns="False" 
        AllowAutomaticUpdates="True" DataSourceID="SqlDataSource1" OnItemCommand="RadGrid1_ItemCommand"

    Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand 
            If (e.CommandName = RadGrid.UpdateCommandName) Then 
                Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem) 
                SqlDataSource1.UpdateParameters("Color").DefaultValue = ColorTranslator.ToHtml(CType(editedItem.FindControl("RadColorPicker1"), RadColorPicker).SelectedColor) 
            End If 
    End Sub 

    I have attached my test page for your reference as well.

    Kind regards,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft R3 2016 release webinar banner
Back to Top