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

GridViewColorColumn Changes

8 Answers 226 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Art
Top achievements
Rank 1
Art asked on 28 Oct 2015, 02:31 AM

I'm creating a GridViewColorColumn and I would like to modify it's look. I just want the color box showing the color and the button, no text. It would be nice if I could make the color box wider also.

 VB code please.

   Private Sub grdMainGrid_DataBindingComplete(sender As Object, e As GridViewBindingCompleteEventArgs) Handles grdMainGrid.DataBindingComplete

        Dim colorBoxColumn As New Telerik.WinControls.UI.GridViewColorColumn
        colorBoxColumn.DataType = GetType(Color)
        colorBoxColumn.HeaderText = "Staff Color"
        grdMainGrid.Columns(2) = colorBoxColumn
        grdMainGrid.AllowEditRow = True
        grdMainGrid.Columns(2).ReadOnly = False

​end sub

 

Later

Art

8 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2015, 02:02 PM
Hello Art,

Thank you for writing.

In order to achieve your goal, you can use a custom GridColorCellElement. Here is a sample implementation which result is illustrated on the attached screenshot:
public Form1()
{
    InitializeComponent();
 
    this.radGridView1.CreateCell += radGridView1_CreateCell;
 
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Color", typeof(Color));
 
    dt.Rows.Add(1, "Item1", Color.Red);
    dt.Rows.Add(2, "Item2", Color.Yellow);
    dt.Rows.Add(3, "Item3", Color.FromArgb(124, 25, 78));
 
    this.radGridView1.DataSource = dt;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
 
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridColorCellElement))
    {
        e.CellElement = new CustomGridColorCellElement(e.Column, e.Row);
    }
}
 
public class CustomGridColorCellElement : GridColorCellElement
{
    public CustomGridColorCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
        this.DrawText = false;
        this.ColorBox.StretchHorizontally = true;
    }
    
    protected override Type ThemeEffectiveType    
    {
        get   
        {
            return typeof(GridColorCellElement);    
        }
    }
     
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        RectangleF clientRect = this.GetClientRectangle(finalSize);
 
        if (this.Editor != null)
        {
            this.ArrangeEditorElement(finalSize, clientRect);
            return finalSize;
        }
 
        RectangleF colorBoxRect = clientRect;
        this.ColorBox.Arrange(colorBoxRect);
 
        RectangleF lmpRect = new RectangleF(this.ColorBox.DesiredSize.Width, clientRect.Y,
            clientRect.Width - this.ColorBox.DesiredSize.Width, clientRect.Height);
 
        this.Layout.Arrange(lmpRect);
        return finalSize;
    }
}

Our GridView >> Creating custom cells help article is quite useful on this topic.

I hope this information helps. Should you have further questions I would be glad to help.
 

Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Art
Top achievements
Rank 1
answered on 28 Oct 2015, 02:29 PM
Ok, thanks, is there any way I can get that in VB code.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2015, 02:43 PM
Hello Art,

Thank you for writing back.

Here is the sample code snippet in VB.NET:
Public Sub New()
    InitializeComponent()
 
    AddHandler Me.RadGridView1.CreateCell, AddressOf radGridView1_CreateCell
 
    Dim dt As New DataTable()
    dt.Columns.Add("Id", GetType(Integer))
    dt.Columns.Add("Name", GetType(String))
    dt.Columns.Add("Color", GetType(Color))
 
    dt.Rows.Add(1, "Item1", Color.Red)
    dt.Rows.Add(2, "Item2", Color.Yellow)
    dt.Rows.Add(3, "Item3", Color.FromArgb(124, 25, 78))
 
    Me.RadGridView1.DataSource = dt
    Me.RadGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
End Sub
 
Private Sub radGridView1_CreateCell(sender As Object, e As GridViewCreateCellEventArgs)
    If e.CellType = GetType(GridColorCellElement) Then
        e.CellElement = New CustomGridColorCellElement(e.Column, e.Row)
    End If
End Sub
 
Public Class CustomGridColorCellElement
Inherits GridColorCellElement
    Public Sub New(column As GridViewColumn, row As GridRowElement)
        MyBase.New(column, row)
        Me.DrawText = False
        Me.ColorBox.StretchHorizontally = True
    End Sub
 
    Protected Overrides ReadOnly Property ThemeEffectiveType() As Type
        Get
            Return GetType(GridColorCellElement)
        End Get
    End Property
 
    Protected Overrides Function ArrangeOverride(finalSize As SizeF) As SizeF
        Dim clientRect As RectangleF = Me.GetClientRectangle(finalSize)
 
        If Me.Editor IsNot Nothing Then
            Me.ArrangeEditorElement(finalSize, clientRect)
            Return finalSize
        End If
 
        Dim colorBoxRect As RectangleF = clientRect
        Me.ColorBox.Arrange(colorBoxRect)
 
        Dim lmpRect As New RectangleF(Me.ColorBox.DesiredSize.Width, clientRect.Y, clientRect.Width - _
                                      Me.ColorBox.DesiredSize.Width, clientRect.Height)
 
        Me.Layout.Arrange(lmpRect)
        Return finalSize
    End Function
End Class

Feel free to use our online Code Converter.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Art
Top achievements
Rank 1
answered on 28 Oct 2015, 04:26 PM

Thanks, got it working.

Later

Art

0
Art
Top achievements
Rank 1
answered on 28 Oct 2015, 05:17 PM
Quick question, why when I choose a color from the "Professional" tab, it doesn't show up in the color box? What's up with that?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Oct 2015, 11:48 AM
Hello Art,

Thank you for writing back.

When using the provided code snippet from my previous post and I select a color from the Professional tab, the selected color is successfully displayed in the color box. Please refer to the attached gif file illustrating the behavior on my end. Am I missing something? Could you please specify the exact steps how to reproduce the problem? Once, we replicate the issue locally, we can make an adequate analysis of the problem and assist you further. Thank you in advance.

I am looking forward to your reply.

 Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Top achievements
Rank 1
answered on 17 Mar 2016, 01:53 AM

Hi Dess,

On this basis, How can i custom the GridColorPikckerElement shows when click in order to hide the RadTextBoxItem?

in other words ,when i click the cell,it only shows  colorbox and colorpickerbutton,not textbox for input.

looking forward to you reply.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Mar 2016, 03:09 PM
Hello ,

Thank you for writing back. 

In order to hide the text box and enlarge the color box, you can subscribe to the RadGridView.CellEditorInitialized event and use the following code snippet:

Private Sub RadGridView1_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
    Dim colorPicker As GridColorPickerEditor = TryCast(e.ActiveEditor, GridColorPickerEditor)
    If colorPicker IsNot Nothing Then
        Dim el As GridColorPickerElement = TryCast(colorPicker.EditorElement, GridColorPickerElement)
        el.TextBoxItem.Visibility = Telerik.WinControls.ElementVisibility.Collapsed
        el.ColorBox.MinSize = New Size(e.Column.Width - el.ColorPickerButton.Size.Width - 20, 0)
    End If
End Sub

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Art
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Art
Top achievements
Rank 1
Top achievements
Rank 1
Share this question
or