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

one column on the grid is moving

5 Answers 82 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lily
Top achievements
Rank 1
Lily asked on 25 Feb 2011, 10:43 PM
Hi,
I have a gridvie which i sho in two modes some columns change visibility
:

 

if

(_local)

 

radGridView1.Columns[

"colFirstSeen"].IsVisible = true;

 

 

radGridView1.Columns[

 

"colTrust"].IsVisible = true;
radGridView1.Columns["colAcknowledged"].IsVisible = true;;

 

 

 

 

}

 

else

 

 

 

 

 

 

 

{

radGridView1.Columns[

"colFirstSeen"].IsVisible = false
radGridView1.Columns["colFirstSeen"].VisibleInColumnChooser = false

radGridView1.Columns["colTrust"].IsVisible = false

;

 

 

 

radGridView1.Columns[

"colAcknowledged"].IsVisible = false

 

}

When I change the value of local from true to false and than back to true the column "colFirstSeen", which is last is moving to the right
The image is attached.
Thank you
Lily

 

 

Sorry, when I save the poste it changes the fomat.

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 10:53 PM
Hello Lily,

from the information that you've provided, may i ask which AutoSizeColumnsMode you are using? I.e.
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
// or
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;

If you are using None then are you performing BestFitColumns as this will cause columns to move to allow them to resize for the content.
regards,
Richard
0
Lily
Top achievements
Rank 1
answered on 25 Feb 2011, 11:03 PM

this

 

.radGridView1.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

 


I don't know what BestColumnFit does and if we are using it.
0
Richard Slade
Top achievements
Rank 2
answered on 25 Feb 2011, 11:34 PM
Hi Lily,

From the information that you have provided it's not possible to draw a conclusion. If you are able to replicate this in a simple test project, then please post that here so I can review it for you and I'll be happy to help
Thanks
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Feb 2011, 12:29 PM
Hello Lily, Richard,

A few things before i offer some alternatives.
First of, i don't really like the BestFitColumns(), lately it is becoming very unstable
[Just open any grid and right click on the header and select best fit, do it again and again, and you will see that the column size keeps changing... kinnda strange considering it should be best fit, but maybe this is just me].

So, getting back to the problem at hand, the first alternative would be to just calculate the minimum size required for the column in order for it to display all the data inside that column. In this other thread i have provided some options to calculate the best possible widths of a column (or all the columns). A solution, this time for your exact case could go something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Size = new System.Drawing.Size(800, 600);
 
        InitGrid();
        LoadDataSource();
 
        var button = new RadButton();
        this.Controls.Add(button);
        button.Text = "Change date column visibility";
        button.Dock = DockStyle.Bottom;
        button.Click += new EventHandler(button_Click);
    }
 
    void button_Click(object sender, EventArgs e)
    {
        radGridView1.Columns["Date"].IsVisible = !radGridView1.Columns["Date"].IsVisible;
 
        if (!radGridView1.Columns["Date"].IsVisible)
        {
            return;
        }
 
        var maxString = radGridView1.Rows.Select(r => r.Cells["Date"].Value != null ? r.Cells["Date"].Value.ToString() : string.Empty).Max();
        double widthRequired = 0;
        using (var graphics = radGridView1.CreateGraphics())
        {
            widthRequired = graphics.MeasureString(maxString, radGridView1.Font).Width * 1.4;
        }
 
        radGridView1.Columns["Date"].Width = Convert.ToInt32(widthRequired);
    }
 
    private void LoadDataSource()
    {
        var list = new List<Employee>();
        for (int i = 0; i < 20; i++)
        {
            list.Add(new Employee { Id = i, Name = "Employee" + i, Enabled = i % 3 == 0, Date = DateTime.Now.AddHours(i) });
        }
 
        radGridView1.DataSource = list;
    }
 
    private void InitGrid()
    {
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    }
}
 
public class Employee
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public bool Enabled { get; set; }
 
    public DateTime Date { get; set; }
}
Please test it and tell me if this suits your needs.

If you have any other questions i would be more than happy to assist in any way that i can.

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 26 Feb 2011, 12:48 PM
Hello again,

Or you can use a cleaner version that uses custom properties for the column to store the old width of the column before hiding and restoring the column to that old width when making it visible again, like the following:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Size = new System.Drawing.Size(800, 600);
 
        InitGrid();
        LoadDataSource();
 
        var button = new RadButton();
        this.Controls.Add(button);
        button.Text = "Change date column visibility";
        button.Dock = DockStyle.Bottom;
        button.Click += new EventHandler(button_Click);
    }
 
    void button_Click(object sender, EventArgs e)
    {
        ResizeJustDateColumn();
        //ResizeAllColumns();
    }
 
    private void ResizeJustDateColumn()
    {
        var column = radGridView1.Columns["Date"];
        if (column.IsVisible)
        {
            column.SetValue(CustomColumnProperty.OldWidthOfTheColumn, column.Width);
        }
 
        column.IsVisible = !column.IsVisible;
 
        if (!column.IsVisible)
        {
            return;
        }
 
        column.MinWidth = (int)column.GetValue(CustomColumnProperty.OldWidthOfTheColumn);
        column.Width = column.MinWidth;
        Application.DoEvents();
        column.MinWidth = 5;
        column.MaxWidth = 0;
    }
 
    private void ResizeAllColumns()
    {
        var requiredColumn = radGridView1.Columns["Date"];
        if (requiredColumn.IsVisible)
        {
            foreach (var column in radGridView1.Columns)
            {
                column.SetValue(CustomColumnProperty.OldWidthOfTheColumn, column.Width);
            }
        }
 
        requiredColumn.IsVisible = !requiredColumn.IsVisible;
 
        if (!requiredColumn.IsVisible)
        {
            return;
        }
 
        foreach (var column in radGridView1.Columns)
        {
            column.MinWidth = (int)column.GetValue(CustomColumnProperty.OldWidthOfTheColumn);
            column.Width = column.MinWidth;
            Application.DoEvents();
            column.MinWidth = 5;
            column.MaxWidth = 0;
        }
    }
 
    private void LoadDataSource()
    {
        var list = new List<Employee>();
        for (int i = 0; i < 20; i++)
        {
            list.Add(new Employee { Id = i, Name = "Employee" + i, Enabled = i % 3 == 0, Date = DateTime.Now.AddHours(i) });
        }
 
        radGridView1.DataSource = list;
    }
 
    private void InitGrid()
    {
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    }
}
 
public static class CustomColumnProperty
{
    public static readonly RadProperty OldWidthOfTheColumn =
        RadProperty.Register("OldWidthOfTheColumn", typeof(int), typeof(GridViewColumn), new RadElementPropertyMetadata(0));
}
 
public class Employee
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public bool Enabled { get; set; }
 
    public DateTime Date { get; set; }
}

I have provided two different approaches here, the first and the active one here just takes the width of the required column and restores it when making it visible again, the second approach takes the width of all the columns, and restores all of them when making that column visible.

I believe that with this info you will be able to develop a solution that best fit your specific need.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
Tags
GridView
Asked by
Lily
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Lily
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or