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

Binding to Cell of GridView and datacontext says DataRow

4 Answers 402 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eli
Top achievements
Rank 1
Eli asked on 24 Oct 2012, 06:25 PM
Hello,


We are using 2012 Q3
I am AutoGenerating columns from a datatable to populate a RadGridView.

In the table there are columns who's DataType is a tuple with a couple strings, defined as "characteristic columns".

I tried setting the cell template using the following code,

        private void OneStopGrid_DataLoaded(object sender, EventArgs e)
        {
            foreach (GridViewColumn column in OneStopGrid.Columns)
            {
                if (ViewModel.CharacteristicColumns.Any(c => c.Name == column.Header))
                {
                    column.CellTemplate = (DataTemplate)this.FindResource("HistoryTemplate");
                }
            }
        }

The history template is a stackpanel containing a textblock which basically binds to one of the strings using Text="{Binding Item1}", and a rectangle which is standing in for a button.

The cell template is correctly set, but the datacontext is wrong, when I set the binding to Text="{Binding}" it showed that in the cells the datacontext was the DataRow.

I looked on the forums and found

http://www.telerik.com/community/forums/wpf/gridview/programmatically-creating-templated-columns.aspx

which advised me to to go to

http://blogs.telerik.com/vladimirenchev/posts/10-05-13/how-to-custom-percentage-column-with-radgridview-radprogressbar-and-radslider-for-silverlight-and-wpf.aspx

I set up a custom column as recommended and overrode the CreateCellElement using the following code


Then I used the ColumnsGenerating event to set up the columns as my custom column

        private void OneStopGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            if (ViewModel.CharacteristicColumns.Any(c => c.Name == e.Column.Header))
            {
                var templateColumn = new GridViewCharacteristicColumn();
                templateColumn.Header = e.Column.Header;

                e.Column = templateColumn;
            }
        }

Unfortunately I am still having the same problem. The Context is still the DataRow.

Anything I can do to get the binding to work correctly?


Thank you.

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 25 Oct 2012, 01:39 PM
Hello,

Please keep in mind that the DataContext of the cells in a GridView Column is the data item bound to their parent row.  If your rows are bound to DataRows, then a DataRow will be the DataContext for the cells in the rows.

In case you would like to have a different DataContext, then you could specify it as a Source for the Binding using a StaticResource. 

I hope this explains the case.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Eli
Top achievements
Rank 1
answered on 25 Oct 2012, 05:53 PM
What source would set the binding for that cell to the value of the datarow under the column it is being populated in?
0
Vlad
Telerik team
answered on 26 Oct 2012, 06:21 AM
Hello,

 The source is the the data item associated with the cell/row (in your case DataRow). 

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Eli
Top achievements
Rank 1
answered on 29 Oct 2012, 08:41 PM
Is there a way to make the DataTemplate work on items in the cells?
I have populated about half the columns with a view model, but templating them thus does not seems to work

<DataTemplate DataType='{x:Type ViewModel:MyViewModel}'>
            <TextBlock Text="{Binding TheContextINeed}"
                       VerticalAlignment="Center"
                       Margin="5,0" />
        </DataTemplate>
Tags
GridView
Asked by
Eli
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Eli
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or