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

[Solved] Binding data to radgridview

9 Answers 528 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Uyen Thao
Top achievements
Rank 1
Uyen Thao asked on 06 Mar 2009, 07:46 AM
Hi,

I have created an object (example : Customer object) with some properties such as: ID (int), Name (string), Status (bool), CompanyID (int)..., and then i've created a list of Customer to bind the radgridview.
I've also created another object (example: Company object) with two properties: CompanyID (int), CompanyName(string), initialized a list data to bind to radcombobox.
The radgridview is bound when page's loaded at the first time or when an item in radcombobox is selected.
When all Customer's properties are basic types, the radgridview works well.

After that, i have changed Customer object with some properties such as: ID (int), Name (string), Status (RadCombobox), CompanyID (int)..., created a list of Customer, and bound to the radgridview's ItemsSource.
The first time when page's loaded, the radgridview has worked well with data has been bound.
But at the second time, after i've chosen an item in radcombobox to bind datat to the radgridview again, an error is raised.

{System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridView.GridViewCellBase.DataColumn_ColumnWidthChanged(Object sender, ColumnWidthChangedEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Telerik.Windows.Controls.GridViewColumn.RaiseColumnWidthChanged(GridLength newWidth)
   at Telerik.Windows.Controls.GridView.GridViewCellBase.set_RelatedColumnDefinition(ColumnDefinition value)
   at Telerik.Windows.Controls.GridView.DataCellsPresenter.CreateItems(Grid grid)
   at Telerik.Windows.Controls.GridView.DataCellsPresenter.CreateDataCells()
   at Telerik.Windows.Controls.GridView.GridViewRowItem.CreateFields()
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.PopulateHeaderRow()
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.InitializeDataSource()
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.Initialize()
   at Telerik.Windows.Controls.GridView.RadItemsControl.OnRecordManagerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.GridView.RadItemsControl.set_RecordManager(RecordManager value)
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.ConfigureRecordManager()
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.InitializeRecordManager()
   at Telerik.Windows.Controls.GridView.GridViewItemsControl.GridViewItemsControl_Loaded(Object sender, RoutedEventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)}    System.Exception {System.NullReferenceException}


Would you please to tell me what's wrong and how to fix this error?
I look forward to your help. Thank you very much

Best regards,
Uyen Thao





9 Answers, 1 is accepted

Sort by
0
Hristo Deshev
Telerik team
answered on 07 Mar 2009, 11:08 AM
Hi Uyen Thao,

I tried reproducing the problem, but with no success. I am able to add a property of the RadComboBox type to my data objects, and when binding the grid, I get the combo boxes in my cells.

I am attaching my project -- can you reproduce the problem there? What am I missing?

Regards,
Hristo Deshev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Uyen Thao
Top achievements
Rank 1
answered on 09 Mar 2009, 07:38 AM
Hi Hristo Deshev,

I have sent a support ticket about this problem and I've also attached your project with some change in the code. Please see it in Support. Thank you very much.

Best regards,
Uyen Thao
0
Hristo Deshev
Telerik team
answered on 09 Mar 2009, 12:19 PM
Hello Uyen Thao,

Thanks to the project in your support ticket, I reproduced the problem, and I am posting our findings here, so that others can avoid this problem.

Unfortunately the cause looks like a limitation of the Silverlight framework -- I have seen this exception in the past when setting some visual elements as a ContentControl's Content value. GridViewCell is a ContentControl, and the grid will automatically set your property value as the cell content.

I think the best approach is to set a data object as the content, and provide a CellStyle with a template that will create a combobox and bind it to data coming from your object. Here is a sample style:

<GridView:GridViewDataColumn> 
    <GridView:GridViewDataColumn.CellStyle> 
        <Style TargetType="GridView:GridViewCell"
            <Setter Property="Template"
                <Setter.Value> 
                    <ControlTemplate TargetType="GridView:GridViewCell"
                        <Controls:RadComboBox ItemsSource="{Binding StatusValues}"></Controls:RadComboBox>                                         
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
    </GridView:GridViewDataColumn.CellStyle> 
</GridView:GridViewDataColumn> 
 

Note that I have changed the Status property to a contain a collection of Statuses. You may want to bind the combo box to some other collection and set the selected item with a binding to your Status property -- it depends on your current scenario. The complete sample project is available here.


Kind regards,
Hristo Deshev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Uyen Thao
Top achievements
Rank 1
answered on 11 Mar 2009, 01:00 AM
Hi Hristo Deshev,

Thank you for your support. It's helped me a lot.

Best regards,
Uyen Thao
0
Uyen Thao
Top achievements
Rank 1
answered on 12 Mar 2009, 08:36 AM
Hi Hristo Deshev,

Thanks for your sample project, now i can bind data to the radgridview with no problem, but i have another problem. I don't know how to get SelectedIndex of the radcombobox in the radgridview.
If you don't mind, would you please to tell me how to do it?
I'm looking forward to your help. Thank you very much.

Sincerely yours,
Uyen Thao
0
Vlad
Telerik team
answered on 12 Mar 2009, 04:47 PM
Hello Uyen Thao,

You  can use SelectionChanged event of the combo - sender argument is the combo box.

Regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Uyen Thao
Top achievements
Rank 1
answered on 13 Mar 2009, 08:10 AM
Hello Vlad,

Thanks for your help. It's very kind of you to help me. According to your guide, i've gotten the selectedIndex value of the radCombobox. But i don't know where the radcombobox's in the radgridview. Could you show me how to get the current row of the radcombobox in the radgirdview?
Thank you very much.

Best regards,
Uyen Thao


0
Vlad
Telerik team
answered on 16 Mar 2009, 03:37 PM
Hello Uyen,

Generally you can use the same event to get the grid row with our extension methods:

GridViewRow row = ((RadComboBox)sender).ParentOfType<GridViewRow>();

All the best,
Vlad
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Uyen Thao
Top achievements
Rank 1
answered on 20 Mar 2009, 10:22 AM
Hi Vlad,

Thank you for your support. It is a good solution, I have solved my problem

Best regards,
 Uyen Thao
Tags
GridView
Asked by
Uyen Thao
Top achievements
Rank 1
Answers by
Hristo Deshev
Telerik team
Uyen Thao
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or