DataGridTextColumn no longer accepts '*' as width.

Oct 28, 2008 at 3:35 PM
Edited Oct 28, 2008 at 4:07 PM
If I try to set a datagridtextcolumn's width property to '*'. Then I get the compile error: "Value should not be infinity Parameter name: desiredValue".  If this bug is not caught by the designer then it results in an unhandled exception at runtime.
Oct 29, 2008 at 9:11 PM
Edited Oct 29, 2008 at 9:14 PM

I have run into same issue. The DataGrid no longer accepts * for width. Is there any way to fix it?

Coordinator
Oct 29, 2008 at 11:36 PM
I'm not able to repro this on my machine.  Could you post the markup from a simple repro application?  This sounds like it could be a bug. 

Also, are you both using the designer?  If so, which version of Visual Studio are you using?  Thanks!
Oct 30, 2008 at 1:58 PM
I can provide more details:
If the layout isn't constrained in width, a grid with a star column throws with "Value should not be infinity. Parameter name: desiredValue".
The details are that availableWidth = infinity, then the grid computes that the star column's desiredWidth is infinity as well.

There are lots of way to reproduce this:
- In the Visual Studio designer, be sure that the element you're designing doesn't have a width or a maxwidth. This is typically the case for UserControl. It may then work in your application (where it is constrained) but not in the VS designer (unconstrained).
- Place the grid inside a Canvas. Canvas offer unlimited space to its children.
Here's some code, if you need a repro:
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    MinHeight="376" MinWidth="386">
   
    <tk:DataGrid x:Name="grid" AutoGenerateColumns="False">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Width="Auto" Header="Nom (auto)" Binding="{Binding Name}" />
            <tk:DataGridTextColumn Width="Auto" Header="Prénom (auto)" />
            <tk:DataGridTextColumn Width="100" Header="Téléphone (100)" />
            <tk:DataGridTextColumn Width="*" Header="Description (*)" />
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Window>

I would like to point out another bug with the star-sized columns. I didn't have the time to check out the details on this one, though.
Apparently, when you have both "Auto" and "*" columns in the same grid (as in the example above), if the grid is empty the "Auto" columns are incorrectly sized. They are extremely small. You can see this bug in the repro above as well. As soon as you put some data in the grid, the problem disappears.
Coordinator
Oct 30, 2008 at 6:07 PM
Thanks for reporting and for the details.  I am able to reproduce this and we now have a bug tracking the issue.  As a work around you can set the Width and Height on your window explicitly and you will not see the exception. 

For example, change the xaml above to:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Height="376" Width="386">
   
    <tk:DataGrid x:Name="grid" AutoGenerateColumns="False">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Width="Auto" Header="Nom (auto)" Binding="{Binding Name}" />
            <tk:DataGridTextColumn Width="Auto" Header="Prénom (auto)" />
            <tk:DataGridTextColumn Width="100" Header="Téléphone (100)" />
            <tk:DataGridTextColumn Width="*" Header="Description (*)" />
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Window>
Oct 31, 2008 at 9:23 AM
Yes, I reached the same conclusion: as a workaround, one can constraint the grid either with some Width somewhere in the hierarchy, or even just MaxWidth.

Note that while it's OK to set a width on a window (and there's one by default), it is much less practical if we're talking about a UserControl, which should fit its (future) containers.

Please don't forget the bug about Auto being incorrectly sized when there's a star and the grid is empty!
Nov 3, 2008 at 1:32 PM
Edited Nov 3, 2008 at 8:23 PM

Even this way is not working. I'm getting 'availibleStarSpace is not valid... at DataGridColumnCollection.ComputeStarColumnWidths(Double availableStarSpace)'. And I specified Window Width and Height. 

In previous version of the DataGrid, I’ve used percentage width (Example: With=.20*) and it worked. Now it’s impossible. Percentage width gave more flexible layout, and columns was sized accordingly Window size.  

                                                                                                 Thanks. Lena

Nov 4, 2008 at 3:28 PM

If DataGrid width is specified, it accepts * and percentage with also. Good control!

                                                                                                    Thanks. Lena

Coordinator
Nov 5, 2008 at 2:35 AM
Another workaround would be to set the max width on the column rather than setting the width on user control / window.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    MinHeight="376" MinWidth="386">
   
    <tk:DataGrid x:Name="grid" AutoGenerateColumns="False">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Width="Auto" Header="Nom (auto)" Binding="{Binding Name}" />
            <tk:DataGridTextColumn Width="Auto" Header="Prénom (auto)" />
            <tk:DataGridTextColumn Width="100" Header="Téléphone (100)" />
            <tk:DataGridTextColumn Width="*" MaxWidth="500" Header="Description (*)" />
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Window>

I have set the MaxWidth as 500 here, but you could choose what ever is appropriate for you. This way the star column's width would be restricted to the set MaxWidth when the DataGrid is given infinite width like in designer and canvas (and wouldnt crash). And other places would continue to run appropriately as they now except for with MaxWidth constraint. The column in designer would appear big if you set the MaxWidth very high but that is by definition of star (take as much as is available).
Nov 5, 2008 at 4:09 AM
I am getting the same problem as "jdu".... like .... ("SizeToHeader" being incorrectly sized when there's a star and the grid is empty! ).


Nov 6, 2008 at 2:17 AM
Is there going to be a fix for this anytime soon?  If not I will have to revert to the CTP version of the datagrid.
Coordinator
Nov 6, 2008 at 6:07 PM
There won't be a fix available until .NET 4.0, which won't be available until at least in late 2009.  Do none of the work-arounds suggested work for your scenario?
Nov 7, 2008 at 8:06 AM
Edited Nov 7, 2008 at 8:08 AM

The grid will not be updated anymore before late 2009?!

I sincerely hope you will reconsider this... The grid still has several bugs, which prevent its use. I am really annoyed myself by the following ones:
- "*" raise exceptions on unbounded grid's width. _There's no good workaround for this._ I have a window, which works in the designer when it's loaded. But then I don't know what the designer do when it updates itself, but the grid crashes. This completely breaks my design-time experience. And I expect this is a quite common scenario... many users are less patient than the ones posting here, and will just drop your grid.
- Mixing "*" columns and "Auto" (or "SizeToHeader") is broken when the grid is empty. This breaks user experience.
- Recycling virtualized mode without row selector is plain broken. When you scroll down, some random rows are offset to the right. They seem to have a row selector (although they shouldn't).

This is out-of-band, CTP stuff. You'll have to fix those bugs anyway... What prevents you from publishing a fixed release? Why do you have to wait one year (at least) until the 4.0 release to fix those bugs?

I am willing to try new MS stuff, help fix bugs, make the extra effort to use poorly documented and sometimes still buggy libraries. But I am only doing so because I have the confidence that MS is responsive to our feedback. It's useless to publish a library mid-2008, encourage adoption (e.g. in MS blogs) and have a big blank until end-2009. The grid is not production-ready yet, please continue to make updates to this project.

Coordinator
Nov 7, 2008 at 10:20 PM
We understand your frustration and will release an update if it's at all possible.  The team is heads down working on features for .NET 4.0 and finishing the new Ribbon control, and unfortunately there just aren't enough hours in the day to get it all done as soon as we'd like.  Due to a division-wide schedule that we're subject to for these other features, we likely won't have an opportunity to fix any DataGrid bugs for at least another 6 months. 

I know that seems like a long time off, but you have to understand that the team working on DataGrid is also responsible for many other WPF features and simply cannot devote all of our time to this one control.  As much as we'd like to make it perfect now, the rest of the platform would suffer if we did that.  We understand that DataGrid in its current form is far from perfect, but it has been able to get most users up and running and we've released the source code so that if you run into anything major that we can't fix immediately, you can ahead and modify it yourself.  I assure you that we are working hard to get these bugs fixed as soon as we possibly can and will release an update if the timing works out. 

That being said, we can't make any promises about releasing updates before .NET 4.0 which is our only definite, planned update at this time.  Thank you for your patience and for bearing with us as we finalize this new control.  All of the feedback which we receive from you and our other customers and partners is being taken into account, filed as bugs, and fixed as soon as we can. 

Thanks,
Samantha
Nov 11, 2008 at 8:21 PM
Edited Nov 11, 2008 at 8:23 PM
Samantha, thank you for your quick responses.

I have a Window hosting a UserControl hosting a DataGrid.  Assigning a maxwidth on the columns where I am assigning '*' to width works as a work-around, meaning it does produce an unhandled exception. 

However, the vertical scroll bar always appears on the DataGrid.  This is because the column doesn't re-size (to '*') correctly when the window or UserControl is re-sized (Where both are re-sizable).  The vertical scroll bar is not very aesthetically pleasing and hides an 'Open' Button we have housed in the last column.  Has anyone else experienced this and/or come up with a fix/work-around?  I can post a sample if needed.

That being said, a fix would REALLY be appreciated and would benefit a lot of people.

Coordinator
Nov 12, 2008 at 12:47 AM
Edited Nov 12, 2008 at 1:06 AM
Hi Cirem and Others,

I would try to define the actual problem in my words. First of all I agree that DataGrid should not crash when there are * columns and if DataGrid is given infinite width. Having said that, by setting the width of the column to * we are asking the column to take away all the remaining available space (That is the definition of our star columns). The problem here is that * column is trying to take infinite space (because that is what is available to it), which crashes. But aren't we asking it to do the same? Anyways lets speak about the work arounds

Now the two suggested workarounds were to
1) Fix the Width of the Window/UserControl
2) Set the MaxWidth of the Column
Both these approaches work because basically they are asking the * column to limit its width (not to set itself to infinity). Approach #1 makes it hard for run time where you dont want to hard code the width, expecially with UserControls. Approach #2 makes it hard for the design time where the column appears as large as its MaxWidth. So let us try to solve this problem in a different way.....

The problem could be divided into two categories 1) DesignTime and 2)RunTime
1)  Design Time: The workaround I suggest here is to fix the width of the user control for design time, such that it doesnt affect the run time. This can be done as follows (concentrate on bold code)...

<UserControl x:Class="DataGridM1Demo.UserControl1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400"> <!-- Set Width -->

    <StackPanel>

        <dg:DataGrid>

            <dg:DataGrid.Columns>

                <dg:DataGridTextColumn Width="*" MaxWidth="500" Header="hello"></dg:DataGridTextColumn> <!-- Set Max Width -->

            </dg:DataGrid.Columns>

        </dg:DataGrid>

    </StackPanel>

</UserControl>

 

namespace DataGridM1Demo

{

    /// <summary>

    /// Interaction logic for UserControl1.xaml

    /// </summary>

    public partial class UserControl1 : UserControl

    {

        public UserControl1()

        {

            InitializeComponent();
            //Clear the width if not design time.

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))

            {

                ClearValue(WidthProperty);

            }

        }

    }

}

What it means is, if it is not design-time (i.e., run time) clear the value of Width property, hence hardcoded value doesn't get picked up at run time.

2) Run Time: Personally I feel that at runtime DataGrid having a star column should not get infinite width , because they are contradicting in definitions. It is ok with Design Time especially because of the UserControl scenario. But if there is no other option for runtime either but to give infinite width (like putting it in a canvas), then probably you are looking for an Auto width column (which takes as much as needed), not a * column (which takes as much as available). Even if this is not an option (for some reason) then MaxWidth (set as in above) will come to rescue. 

Until it gets fixed I would strongly suggest that DataGrid having * columns should not get infinite width at run time (and should be avoided at design time too if possible.)

Finally, having known the definitions it would be great to get your suggestions on how you would like the DataGrid with * columns to behave when given infinite width.

Hope this helps!!

Nov 13, 2008 at 6:55 PM
Edited Nov 13, 2008 at 7:00 PM
I have gotten past these issues.  My new problem is that often times the vertical scrollbar appears on the grid when it shouldn't.  Has anyone else encountered and come up with a solution/work-around?  I have tried controlling this behavior by handling the events of various PARTS of the DataGrid template, but to no avail. 

Here are some files that replicate the issue:
Also, to see it the bug you have to re-size the window (Although this isn't always the case in our actual project)

'-------The Window------'
<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="400" Width="600" ResizeMode="CanResizeWithGrip">
<Grid x:Name="grdMain">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
</Window>
Class Window1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Dim UCDataGrid As New GridUserControl
UCDataGrid.SetValue(Grid.RowProperty, 1)
UCDataGrid.SetValue(Grid.ColumnProperty, 1)
grdMain.Children.Add(UCDataGrid)
End Sub
End Class

'--------------UserControl with some data----------------'
<UserControl x:Class="GridUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DataGridError="clr-namespace:DataGridError"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" HorizontalAlignment="Stretch" >
<UserControl.Resources>
<x:Array x:Key="incidentList" Type="{x:Type DataGridError:ListIncidentItem}">
<DataGridError:ListIncidentItem IncidentNumber="12345" PatientCareReportNum="5" StreetAddress="905 N 23rd Ave, Duluth, MN 55805" />
<DataGridError:ListIncidentItem IncidentNumber="3456456456465456" PatientCareReportNum="67" StreetAddress="453 e 160th st" />
<DataGridError:ListIncidentItem IncidentNumber="23463" PatientCareReportNum="687" StreetAddress="453 e 160th st" />
<DataGridError:ListIncidentItem IncidentNumber="23463" PatientCareReportNum="687" StreetAddress="453 e 160th st" />
<DataGridError:ListIncidentItem IncidentNumber="23463" PatientCareReportNum="687" StreetAddress="453 e 160th st" />
<DataGridError:ListIncidentItem IncidentNumber="23463" PatientCareReportNum="687" StreetAddress="453 e 160th st" />
<DataGridError:ListIncidentItem IncidentNumber="23463" PatientCareReportNum="687" StreetAddress="453 e 160th st" />
</x:Array>
</UserControl.Resources>

<Grid>
<Controls:DataGrid ItemsSource="{StaticResource incidentList}" AutoGenerateColumns="false" Margin="10" AlternationCount="2" IsReadOnly="True" CanUserSortColumns="False" HorizontalGridLinesBrush="#FFD6D6D6" VerticalGridLinesBrush="#FFD6D6D6" Style="{DynamicResource DataGridStyle1}">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="Incident Number" Binding="{Binding IncidentNumber}" MaxWidth="1000" Width="*" />
<Controls:DataGridTextColumn Header="Patient Care Report Number" Binding="{Binding PatientCareReportNum}" MaxWidth="1000" Width="*" />
<Controls:DataGridTextColumn Header="Address" Binding="{Binding StreetAddress}" MaxWidth="1000" Width="*" />
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Grid>
</UserControl>

'-----------------Dummy Data Object (For your convenience  ;-)  )------------'
Public Class ListIncidentItem

Private myIncidentNumber As String
Private myPatientCareReportNum As String
Private myStreetAddress As String

Public Property IncidentNumber() As String
Get
Return myIncidentNumber
End Get
Set(ByVal value As String)
myIncidentNumber = value
End Set
End Property

Public Property PatientCareReportNum() As String
Get
Return myPatientCareReportNum
End Get
Set(ByVal value As String)
myPatientCareReportNum = value
End Set
End Property

Public Property StreetAddress() As String
Get
Return myStreetAddress
End Get
Set(ByVal value As String)
myStreetAddress = value
End Set
End Property
End Class


'Update: We should be able to control the width of the content anytime 'PART_ScrollContentPresenter' fires the ScrollChangedEvent and the scrollablewidth > 0.
This is what I'm trying now. Editing copy of template > On loaded of window.

Dim mainScroll As ScrollViewer= dgMostRecentIncidentList.Template.FindName("DG_ScrollViewer", dgMostRecentIncidentList)
If mainScroll.IsNotNull Then
AddHandler mainScroll.ScrollChanged, AddressOf MainScrollChanged
End If

Private Sub MainScrollChanged(ByVal sender As Object, ByVal e As ScrollChangedEventArgs)
'TODO: Need to do something until scrollablewidth is 0!
End Sub


Nov 14, 2008 at 3:09 AM
Hi VamseeP, thanks for your solution, but I have bit different problem.

I am using "*" as width for one column and for rest of them I use "SizeToHeader" as width. When (first time) the Grid is empty, the columns with "SizeToHeader" get stretched in like....

<toolkit:DataGrid>
      <toolkit:DataGrid.Columns>

        <toolkit:DataGridCheckBoxColumn Header="Name" Width="SizeToHeader"/>
        <toolkit:DataGridCheckBoxColumn Header="Description" Width="*"/>

      </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>

------------------------------------------------------
N |                                                                            |
a  | Description                                                          |
m |                                                                            |
e  |                                                                            |
------------------------------------------------------


But, if I change Width="SizeToHeader" or "100" or do not use "*" for any column than  it works fine.

Any solution for this?

Thanks

Nov 23, 2008 at 8:22 PM
Samantha, first, I have to say that a direct and honest reply from a software vendor as big as MS is refreshing. I'm so accustomed to worthless boilerplate responses that yours (coupled with ScottGu's general openness and some other things that I've seen happening) make me feel good about this product and the MS culture in general. Compared to working with the Apple's of the world, this is wonderful.

Now, that said, I've never heard of a product cycle that didn't include some form of branching for bug fixing. Even though you're heads down coding for a 4.0 deadline (we all can relate), this is a released and supported product, at least I think it is. This is certainly a very cool product, but the infinite column size issue is painful. It took me hours to figure out the source of the problem (I assumed user error for a long time since I'm very new to WPF) and to then find an answer online. It was doubly frustrating since the basic concept works flawlessly in the core grid component and since I saw and copied many sample code fragments that apparently work with the CTP (finally this is how I got to realize that it must be a v1.0 bug and found this thread). As an aside, note that 10+ minutes were spent cussing while waiting for a "Help Update" that completely blocked all work after mistakenly pressing F1...now that is PAINFUL, though I'm aware that is an issue outside of your team.

I've gotten it working, but only after fixing parent's width (thus it is not actually working as desired--VamseeP's "clear at runtime" solution didn't work for me) and again the productivity loss was pretty significant. If you multiply that by thousands of developers trying to get this v1.0 product working in their environments, then you've got a significant brain drain for what seems to amount to little more than a calculation precedence issue (trying to calculate an infinite column width before a "real" width is assigned to a parent). Likely this is a simple fix that could save countless customer frustration hours and some loss of good will. A twelve month turnaround for a basic bug (essentially a stopping bug in some environments since there's no "real" workaround that achieves the documented level of functionality) is extreme.

If somehow your team can't find the hours to devote to a maintenance release, do you accept code patches if some good samaritan were to isolate and fix the problem? Could this then be incorporated into a 1.01 release? "Someone" might be willing to devote some investigative time to the problem if they thought others might benefit. Forgive me if this is FAQ'ish, but I'm just not sure how "open source" your model is?

Thanks.
Coordinator
Nov 25, 2008 at 3:29 PM

Hi ptyork,

Again, thanks very much for your direct feedback.  We do understand that this is a frustrating issue and are doing all we can to release an update as soon as possible.  I know that this one fix by itself seems a small undertaking, but you have to keep in mind that there are probably 50 other small fixes which are equally important to many developers and if we were to fix all of them, it would turn into a much bigger project.  We really appreciate your suggestion to accept a code patch and are looking into opening up this avenue for the future.  At the current time, though, the best a good samaritan could do is to post the fix to the forums or their own blog so that we could point other users who report this issue to it. 

We hear you, we feel your pain, and we will do our best to release an update which fixes this bug and others as soon as we can.  At this time, the only update I can promise is in .NET 4.0.  That being said, we will look into doing another release prior to that time if at all possible.

Thanks for hanging in there with us and please continue to report any issues you see!

Samantha

Nov 25, 2008 at 4:19 PM
Thanks for your honest and clear answers Samantha. It really helps to know where we stand and what we can expect from MS. I build software for a living and I really need to know what I can use at which time.

I think the labeling was somehow misleading. When MS announces the DataGrid v1 I expect a higher quality. This should have been called a "preview" or "beta" or "ctp", and it should have been clear that there will be no fix or update for a relatively long time. This kind of information is important for business users.

Anyway, your work is appreciated. WPF really needs a nice "advanced" control suite. I'm looking forward to the 4.0 release.
Coordinator
Mar 24, 2009 at 9:59 PM
Update: The issue discussed above (where star columns get a DesiredWidth of infinity) has been fixed in the March 2009 release.
Jun 1, 2009 at 3:21 AM
Edited Jun 1, 2009 at 3:22 AM

The exception has been fixed but it's still not working properly. When placed in a user control with doesn't have a fixed width, setting "*" on column width will cause the column to be extremely wide (several thousand pixel), and this happens both at design time and run time. And this is so easy to repo, just put it in a user control without a fixed with. Is anyone be trapped in this issue as well? What can I do, this is such a popular demand from users.....

Mar 16, 2011 at 3:06 AM

The problem can still be reproduced with the following code:

<Page x:Class="Wpf.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
      Title="Page1">
    <Grid>
        <tk:DataGrid>
            <tk:DataGrid.Columns>
                <tk:DataGridTextColumn Header="0" Width="*"/>
                <tk:DataGridTextColumn Header="1" Width="50"/>
            </tk:DataGrid.Columns>
        </tk:DataGrid>
    </Grid>
</Page>