Read updated colors from a grid

1 Answer 15 Views
ColorPicker Grid
Matthew
Top achievements
Rank 1
Iron
Iron
Matthew asked on 13 Dec 2021, 03:06 PM

Hiya,

I have a grid with a template column that contains a RadColorPicker. I would like to read the color values in a postback.  However, using the code below, I'm only getting the values from the original data source.

ASPX:

<telerik:RadGrid ID="grdUser" runat="server" AllowMultiRowSelection="true" AutoGenerateColumns="false" ShowStatusBar="true"
            AllowSorting="True" AllowFilteringByColumn="true"
            OnItemDataBound="grdUser_ItemDataBound">
            <MasterTableView DataKeyNames="UserId, UserName, HexCode" CommandItemDisplay="None">
                <Columns>
                    <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" HeaderStyle-Width="40px"/>

                    <telerik:GridBoundColumn UniqueName="UserName" HeaderText="Name" DataField="UserName" /> 

                    <telerik:GridTemplateColumn DataField="HexCode" HeaderText="Route Colour" UniqueName="HexCode">
                        <EditItemTemplate>
                            <telerik:RadColorPicker ID="editColor" runat="server" SelectedColor='<%# Bind("HexCode") %>' />
                        </EditItemTemplate>
                         <ItemTemplate> 
                             <telerik:RadColorPicker id="RadColorPicker1" runat="server" ShowIcon="true" /> 
                         </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="true" />
                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            </ClientSettings>
        </telerik:RadGrid>

        <telerik:RadButton ID="btnGo" runat="server" Text="Go"  />

C#:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("UserId", typeof(long));
            dataTable.Columns.Add("UserName", typeof(string));
            dataTable.Columns.Add("HexCode", typeof(string));

            dataTable.Rows.Add(1, "Bob", "#2E8B57");
            dataTable.Rows.Add(2, "Tim", "#BA55D3");
            dataTable.Rows.Add(3, "Jim", "#FF8C00");
            dataTable.Rows.Add(4, "Rob", "#4682B4");
            dataTable.Rows.Add(5, "Dan", "#B22222");

            grdUser.DataSource = dataTable;
            grdUser.DataBind();
        }
        else
        {
            foreach (Telerik.Web.UI.GridDataItem user in grdUser.SelectedItems)
            {
                // Read new color values from the grid.
                string alteredColor = grdUser.MasterTableView.DataKeyValues[user.ItemIndex]["HexCode"].ToString();
            }
        }
    }

    protected void grdUser_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridDataItem item = e.Item as GridDataItem;

        if (item != null)
        {
            RadColorPicker rcpDeviceColour = e.Item.FindControl("RadColorPicker1") as RadColorPicker;

            if (rcpDeviceColour != null)
            {
                string hexCode = item.GetDataKeyValue("HexCode").ToString();
                rcpDeviceColour.SelectedColor = System.Drawing.ColorTranslator.FromHtml(hexCode);
            }
        }
    }

How do I read the updated color values in the grid during a postback?

Thanks in advance,

Matt

1 Answer, 1 is accepted

Sort by
0
Accepted
Matthew
Top achievements
Rank 1
Iron
Iron
answered on 13 Dec 2021, 03:16 PM

I figured it out;


 foreach (Telerik.Web.UI.GridDataItem user in grdUser.SelectedItems)
            {
                Color selectedColour = Color.Blue;

                RadColorPicker colorPicker = user.FindControl("RadColorPicker1") as RadColorPicker;

                if (colorPicker != null)
                {
                    selectedColour = colorPicker.SelectedColor;
                }
                
            }

 

Tags
ColorPicker Grid
Asked by
Matthew
Top achievements
Rank 1
Iron
Iron
Answers by
Matthew
Top achievements
Rank 1
Iron
Iron
Share this question
or