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

CommandCell Button Sizing Issues

1 Answer 155 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sean McConnell
Top achievements
Rank 1
Sean McConnell asked on 24 Jan 2008, 01:49 AM
I have a command column in a gridview.  I am trying to style the button using either the RowFormatting event or the CellFormatting event.  I found some code in one of the posts on how to modify the children and have adapted it to try to get the button styled properly. 

I originally used the CellFormatting event, then I tried the RowFormatting event but both shared the same problem. For some reason, the size of the button is not properly set when the grid renders.  If I click the button, expand another row, or do something to invalidate the button layout, it jumps to the correct size.  I have tried all kinds of things to fix this and explicitly set the button size.

I posted an image that depicts my problem as well:
http://www.thinkconvergence.com/telerik_issue.jpg


Here is a sample of my code from the CellFormatting event:

RadButtonElement button = e.CellElement.Children[0] as RadButtonElement;
if (button != null)
{
button.ForeColor = Color.White;

BorderPrimitive border = ((BorderPrimitive)button.GetChildrenByType(typeof(BorderPrimitive))[0]);
border.ForeColor = Color.FromArgb(0, 87, 42);
border.InnerColor = Color.FromArgb(0, 56, 27);
border.GradientStyle = GradientStyles.Solid;

FillPrimitive fill = ((FillPrimitive)button.GetChildrenByType(typeof(FillPrimitive))[0]);
fill.GradientStyle = GradientStyles.Glass;
fill.BackColor = Color.FromArgb(71, 133, 101);
fill.BackColor2 = Color.FromArgb(71, 133, 101);
fill.BackColor3 = Color.FromArgb(0, 92, 44);
fill.BackColor4 = Color.FromArgb(0, 106, 51);

button.AutoSize = false;
button.Size = new Size(64, 24);  //THIS IS WHAT DOESNT SEEM TO WORK CORRECTLY
button.Margin = new Padding(5);
}

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 24 Jan 2008, 02:55 PM
Hi Randall Kohltfarber,

Thank you for contacting us.

The GridViewCommandColumn expands its content to fill the whole cell space. We suggest creating a new RadButtonElement and adding it inside the cell. Use the MinSize and MaxSize properties when you want to have a constant size.

Refer to the code snippet below:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
    if (e.CellElement.ColumnInfo is GridViewCommandColumn) 
    { 
        if (e.CellElement.Children.Count == 1) 
        { 
            e.CellElement.Children[0].Visibility = ElementVisibility.Collapsed; 
 
            RadButtonElement button = new RadButtonElement(); 
 
            button.ForeColor = Color.White; 
 
            BorderPrimitive border = ((BorderPrimitive)button.GetChildrenByType(typeof(BorderPrimitive))[0]); 
            border.ForeColor = Color.FromArgb(0, 87, 42); 
            border.InnerColor = Color.FromArgb(0, 56, 27); 
            border.GradientStyle = GradientStyles.Solid; 
 
            FillPrimitive fill = ((FillPrimitive)button.GetChildrenByType(typeof(FillPrimitive))[0]); 
            fill.GradientStyle = GradientStyles.Glass; 
            fill.BackColor = Color.FromArgb(71, 133, 101); 
            fill.BackColor2 = Color.FromArgb(71, 133, 101); 
            fill.BackColor3 = Color.FromArgb(0, 92, 44); 
            fill.BackColor4 = Color.FromArgb(0, 106, 51); 
 
            button.AutoSize = true
            button.MinSize = new Size(64, 24);   
            button.MaxSize = new Size(64, 24); 
            button.BypassLayoutPolicies = false
            button.Alignment = ContentAlignment.MiddleCenter; 
            button.Click += new EventHandler(button_Click); 
            e.CellElement.Children.Add(button); 
        } 
    }                 

In case you have other questions, we will be glad to help you.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
Sean McConnell
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or