Delayed columns layout in DataGrid

Nov 28, 2008 at 3:37 AM
Hi everyone,

I'm noticing a problem when my window loads with a datagrid on it. On my window_loaded event, I initialize th datagrid like this:

            dgrdChild.AutoGenerateColumns = false;

            // Columns.
            DataGridTextColumn colAccount = new DataGridTextColumn();
            Binding b = new Binding("Code");
            b.Mode = BindingMode.TwoWay;
            colAccount.Binding = b;
            colAccount.Width = new DataGridLength(100, DataGridLengthUnitType.Star);
            colAccount.CanUserSort = false;
            colAccount.Header = "Code";
            dgrdChild.Columns.Add(colAccount);

            DataGridTextColumn colDescription = new DataGridTextColumn();
            b = new Binding("Name");
            b.Mode = BindingMode.TwoWay;
            colDescription.Binding = b;
            colDescription.Width = new DataGridLength(100, DataGridLengthUnitType.Star);
            colDescription.CanUserSort = false;
            colDescription.Header = "Name";
            dgrdChild.Columns.Add(colDescription);

            DataGridTextColumn colAmount = new DataGridTextColumn();
            b = new Binding("Amount");
            b.Mode = BindingMode.TwoWay;
            colAmount.Binding = b;
            colAmount.Width = new DataGridLength(50, DataGridLengthUnitType.Star);
            colAmount.CanUserSort = false;
            colAmount.Header = "Amount";
            dgrdChild.Columns.Add(colAmount);
            dgrdChild.ItemsSource = _TuitionAccountCol;

 

The problem is, when the window loads, you can see the columns of the datagrid resizing. I mean, you can visually see that all columns will adjust their width. Am I doing something wrong or anyway to avoid this? Thanks.

Coordinator
Dec 9, 2008 at 1:48 PM
Is there any particular reason why you want to do this in the window_loaded event? 
Dec 12, 2008 at 9:18 AM
Edited Dec 12, 2008 at 9:20 AM
Hi Vinsibal,

Sorry for the late response...

No particular reason... I always just did initialize my controls in the window_loaded event... when it comes to other controls like ListView, I don't see this behavior happening so I thought this is OK. But based on your question, am I placing it in the wrong event? I tried putting it on the constructor but I still see this behavior.



Also if I may add a related question:

I have a window with a tabcontrol with 3 tabs. This 3 tabs have content of UserControl, where UserControl has a datagrid inside. In loaded event, I initialize the datagrid like in my original question. Columns are set to:

DataGridLength

(100, DataGridLengthUnitType.Star)

On the first tab, the columns total width occupy the whole length of the grid (as expected). But in both of the next 2 tabs, the horizontal scrollbar appears and width of columns are significantly longer than in the first tab. I don't get it. They all use the same Initializing routine.

If you could guide me, it would be greatly appreciated. Thank you very much.