views:

7976

answers:

5

Hello,

I have a DataGrid Control which is bound to a SQL Table.

The XAML Code is:

<data:DataGrid x:Name="dg_sql_data" 
                       Grid.Row="1" 
                       Visibility="Collapsed" 
                       Height="auto" 
                       Margin="0,5,5,5"
                       AutoGenerateColumns="false"
                       AlternatingRowBackground="Aqua"
                       Opacity="80"
                       >
    <data:DataGrid.Columns>
        <data:DataGridTextColumn Header="Latitude" Binding="{Binding lat}" />
        <data:DataGridTextColumn Header="Longitude" Binding="{Binding long}" />
        <data:DataGridTextColumn Header="Time" Binding="{Binding time}" />
    </data:DataGrid.Columns>
</data:DataGrid>

Is it possible increase the single columns sizes to fill out the complete width of the datagrid?

Thanks,
Henrik

Edit: Columns with "*" as width are coming with the Silverlight SDK 4.

+10  A: 

Solution:

    void dg_sql_data_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        DataGrid myDataGrid = (DataGrid)sender;
        // Do not change column size if Visibility State Changed
        if (myDataGrid.RenderSize.Width != 0)
        {
            double all_columns_sizes = 0.0;
            foreach (DataGridColumn dg_c in myDataGrid.Columns)
            {
                all_columns_sizes += dg_c.ActualWidth;
            }
            // Space available to fill ( -18 Standard vScrollbar)
            double space_available = (myDataGrid.RenderSize.Width - 18) - all_columns_sizes;
            foreach (DataGridColumn dg_c in myDataGrid.Columns)
            {
                dg_c.Width = new DataGridLength(dg_c.ActualWidth + (space_available / myDataGrid.Columns.Count));
            }
        }
    }

Result:

alt text

Henrik P. Hessel
FYI, the picture link is broken
Shimmy
thanks..fixed it
Henrik P. Hessel
+5  A: 

Tested only in WPF, not in Silverlight:

I set up in WPF 3.5 SP1 and it works perfect, no guaranties about Silverlight, but if it works it's indeed charming.

<data:DataGridTextColumn Header="Time" Binding="{Binding}" Width="*" />
Shimmy
Awesome! Works in WPF, not sure about Silverlight
Sergey Aldoukhov
This has a nasty bug, though - if you have a column like this and also right-aligned content in RowDetails, it would not move left when the grid width is decreased.
Sergey Aldoukhov
Yes, this sux, I also experienced it.
Shimmy
A: 

I have created an attached property for the DataGrid that lets you do this in XAML:

<UserControl 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
    x:Class="GridProperties.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:gp="clr-namespace:GridProperties">
<Grid x:Name="LayoutRoot" Background="White">
    <data:DataGrid gp:GridEx.StarColumn="2">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="Column 1"/>
            <data:DataGridTextColumn Header="Column 2"/>
            <data:DataGridTextColumn Header="Column 3"/>
        </data:DataGrid.Columns>
    </data:DataGrid>
</Grid>
</UserControl>
Michael S. Scherotter
A: 

This worked great for me !!! Thanks a lot !!

For me I have GridSplitter in middle and I had DataGrid on left , I was setting Column width to some value in designer , but in run time if user moves Gridsplitter on right , Datagrid 's width was getting new width but column remained same width , as I had column width set to fixed value in designer it was not changing but this fix worked for me ,

Thanks Chirag Patel

Chirag
A: 

I have datagrid width and height are 100% with 4 columns the first column has title with long or short doesn't matter and rest 3 columns are numiric data so, what im looking is the last 3 columns should align right to the datagrid and first column should be left aligned, even when my screen resolution increase or decrease(ex:1024x800 to 1680x1050). http://www.freeimagehosting.net/uploads/ae6816bc9a.jpg, got the solution with in comments only specify width="*" which column should expand It works for me.

Vinod
Stack Overflow is a Question and Answer site, not a discussion forum. If you have a problem then post your own question. Search first to make sure it hasn't already been asked (possibly in a different form).
ChrisF