DataColumnHeader Style in AutoGenerated Grid

May 18, 2009 at 9:17 AM

I just downloaded the March 2009 Toolkit Edition and included a DataGrid into my solution. The Grid auto generates the columns from my object, which worked really fine. In the next step i tried to style the headers:

<toolkit:DataGrid Name="grdResult">
                    <Label>Open it</Label>
            <Style TargetType="{x:Type toolkit:DataGridColumnHeader}">
                <Setter Property="Background">
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                            <GradientStop Color="#FFFFFFFF" Offset="0"/>
                            <GradientStop Color="#FFC4DDFF" Offset="1"/>
                <Setter Property="Padding" Value="3"/>

This results in:

So my questions are:

1) How can I style the RowHeader-ColumnHeader, to look like shown at:

2) Same question for the right most column filling the space left

3) Is there something like AutoSizeColumnsMode="Fill", so that the columns will take all the space and there is no additonal column like in my example?

May 18, 2009 at 1:46 PM
Edited May 18, 2009 at 2:19 PM

This is where you can download the sample:

About autosizing of last column you can use * size. When using * size for columns grid must have set finite Width property or you must have some root element with finite width or you get an exception in design mode. May be it is a bug. I hope there will be someone that can be more comprehensive about column * sizing.

May 18, 2009 at 2:57 PM
Edited May 18, 2009 at 4:16 PM

Thanks for that, but this was not what I needed. They always set which Columns are shown in the Grid, but I don't do. I work with AutoGenerateColumns=true because I'm using the grid to display different types of objects.

So there is no column defintion where I can put a width of "*" to (maybe, if I do this after the Grid has loaded the items ...)

Edit: How do I apply "*" to the Width of a column via Code?

May 18, 2009 at 5:12 PM
Edited May 18, 2009 at 5:34 PM

Hi. You can use  AutoGeneratedColumns event. This event is fired twice: before column generation process and after it. Try this code:


// I do this in page or window constructor

this.dataGrid1.AutoGeneratedColumns += new EventHandler(dataGrid1_AutoGeneratedColumns);


void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)


if (this.dataGrid1.Columns.Count > 0)


this.dataGrid1.Columns[this.dataGrid1.Columns.Count - 1].Width = new DataGridLength(-1, DataGridLengthUnitType.Star);



May 20, 2009 at 7:48 AM

If there would be a "mark as answered" I'd mark firedog's post.