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

Autosize RADGridView

4 Answers 260 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Zerka
Top achievements
Rank 1
Zerka asked on 15 Dec 2010, 10:39 AM
Hi,

I want RadGridView to adjust its size automatically. Like if the height of the rows are less then the maximum height of the grid, grid reduces its height automatically, if rows height are greater than maximum height specified for the grid, then grid sets its height to the maximum height of the grid and shows scrollbar. 

I m doing in this way but it is not working:

dtgBudgetPrognoseRamme.AutoSize = true;

 

dtgBudgetPrognoseRamme.MaximumSize = new Size(525, 192);

 

 

dtgBudgetPrognoseRamme.AutoScrollMinSize = new Size(525, 192);

Any help would be appreciated!

Regards!

4 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 15 Dec 2010, 11:16 AM
Hi Michael,

Try this in a new project. Add to form load. Just a grid on a form

this.RadGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
this.RadGridView1.Columns.Add(new GridViewDecimalColumn("B"));
  
  
GridViewRowInfo rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A1";
rowInfo.Cells[1].Value = 3;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A2";
rowInfo.Cells[1].Value = 4;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A3";
rowInfo.Cells[1].Value = 5;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A4";
rowInfo.Cells[1].Value = 6;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A2";
rowInfo.Cells[1].Value = 4;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A3";
rowInfo.Cells[1].Value = 5;
rowInfo = this.RadGridView1.Rows.AddNew();
rowInfo.Cells[0].Value = "A4";
rowInfo.Cells[1].Value = 6;
  
this.RadGridView1.BestFitColumns();

I wanted to add a screenshot, but the link to add images seems to have gone
Hope that helps
Richard
0
Zerka
Top achievements
Rank 1
answered on 15 Dec 2010, 11:41 AM
Hi,

I have no problem with column width. I just want grid reduces its height automatically if there are few data rows and if there are many rows then i want to set the height of the grid to some maximum value and shows scrollbar.

Although there is a solution on this link but I am thinking that may be now you have some other better solution.

http://www.telerik.com/community/forums/winforms/gridview/adjust-height-of-radgridview-automatically.aspx

Thanks

Regards! 
0
Richard Slade
Top achievements
Rank 2
answered on 15 Dec 2010, 12:09 PM
Hi Michael,

for some reason all my code that I added didn't come out above. Never mind, I understand your requirement now. As far as I'm aware, the link tha tyou provided would still hold true and you should use the solution probided there. There is no built in alternative as far as I know at the moment
Regards,
Richard
0
Jack
Telerik team
answered on 20 Dec 2010, 03:09 PM
Hello Michael,

You cannot restrict the maximum size when RadGridView operates in AutoSize mode. Nevertheless, you can work around the issue by creating a custom table element. You should override its MeasureOverride method to solve the issue. Here is a sample:
radGridView1.MinimumSize = new Size(100, 150);           
radGridView1.AutoSize = true;
radGridView1.ViewDefinition = new CustomViewDefinition();
 
public class CustomViewDefinition: TableViewDefinition
{
    public override IRowView CreateViewUIElement(GridViewInfo viewInfo)
    {
        return new CustomTableElement();
    }
}
 
public class CustomTableElement: GridTableElement
{
    protected override Type ThemeEffectiveType
    {
        get { return typeof(GridTableElement); }
    }
 
    protected override SizeF MeasureOverride(SizeF availableSize)
    {
        SizeF desiredSize = base.MeasureOverride(availableSize);
        if (desiredSize.Width > 525 || desiredSize.Height > 192)
        {
            return base.MeasureOverride(new SizeF(Math.Min(525, desiredSize.Width + SystemInformation.VerticalScrollBarWidth), 192));
        }
        return desiredSize;
    }
}

I added this issue in our bug tracking system and we will improve our API to support this scenario in one of our upcoming releases. I updated also your Telerik points.

Should you have any questions, do not hesitate to ask.

Regards,
Jack
the Telerik team
Check out the Q1 2011 Roadmap for Telerik Controls for Windows Forms.
Tags
GridView
Asked by
Zerka
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Zerka
Top achievements
Rank 1
Jack
Telerik team
Share this question
or