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

[Solved] Error on Add New Record w/ CellValidating

1 Answer 136 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.
Joshua
Top achievements
Rank 1
Joshua asked on 23 Aug 2010, 11:44 AM
I have a GridView with cell validation.  When I click the "Click here to add new item" it creates a new row with a nulled GUID in the ID column and the command button changes from "Delete" to "Cancel".  All of this is fine.

However, the Record (i.e. "RecordName") column does not have a TextBox.  When I try to click anywhere on the row I receive:

Error: Unhandled Error in Silverlight Application Object reference not set to an instance of an object.   at Records.MainPage.rgvRecords_CellValidating(Object sender, GridViewCellValidatingEventArgs e)

Here's my XAML:

<UserControl x:Class="Records.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="1024" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
  
    <Grid x:Name="LayoutRoot" Background="White">
        <telerik:RadGridView HorizontalAlignment="Left" Name="rgvRecords" 
                             VerticalAlignment="Top" ShowGroupPanel="False" 
                             RowIndicatorVisibility="Collapsed"
                             CanUserDeleteRows="True" 
                             ShowInsertRow="True" 
                             CanUserReorderColumns="False" 
                             CanUserSelect="False" 
                             CanUserResizeColumns="False" 
                             AutoGenerateColumns="False" 
                             CanUserFreezeColumns="False" 
                             RowEditEnded="rgvRecords_RowEditEnded"
                             CellValidating="rgvRecords_CellValidating">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="RecordID" DataMemberBinding="{Binding RecordID, Mode=TwoWay}" Width="*" IsFilterable="False" IsReadOnly="True" />
                <telerik:GridViewDataColumn Header="" Width="75" IsFilterable="False">
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <Button Margin="5">Cancel</Button>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <Button Margin="5">Delete</Button>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Record" DataMemberBinding="{Binding RecordName, Mode=TwoWay}" Width="*" IsFilterable="False" />
                <telerik:GridViewDataColumn Header="Active" DataMemberBinding="{Binding Active, Mode=TwoWay}" Width="150">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Active}" IsEnabled="True" />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

NOTE: You'll notice above that there's no template for the Record column in this case.  I've tried it this way and with CellTemplate/CellEditTemplate using a TextBlock and TextBox, respectively.  Both cases present the error.

Part of the Code-behind:

private void rgvRecords_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
        {
            _record = e.EditedItem as Record;
  
            if (e.EditAction == Telerik.Windows.Controls.GridView.GridViewEditAction.Commit)
            {
                if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
                {
                    MyAppClient proxy = new MyAppClient();
                    proxy.AddRecordCompleted += new EventHandler<AddRecordCompletedEventArgs>(proxy_AddRecordCompleted);
                    proxy.AddRecordAsync(_record);
                   
                    rgvRecords.Items.CommitNew();
                }
                else if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Edit)
                {
                    // Edit code
                    rgvRecords.Items.CommitEdit();
                }
            }
  
        }
  
        void proxy_AddRecordCompleted(object sender, AddRecordCompletedEventArgs e)
        {
            _record.RecordID = e.Result;
        }
  
        private void rgvRecords_CellValidating(object sender, Telerik.Windows.Controls.GridViewCellValidatingEventArgs e)
        {
            if (e.Cell.Column.UniqueName.Equals("RecordName"))
                e.IsValid = !string.IsNullOrWhiteSpace(e.NewValue.ToString());
              
        }


Thanks.

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 26 Aug 2010, 01:27 PM
Hi Joshua,

All you have to do is to add UniqueName property to your column definition:

before:

<telerik:GridViewDataColumn Header="Record" DataMemberBinding="{Binding RecordName, Mode=TwoWay}" Width="*" IsFilterable="False" />

after:

<telerik:GridViewDataColumn Header="Record" UniqueName="RecordName" DataMemberBinding="{Binding RecordName, Mode=TwoWay}" Width="*" IsFilterable="False" />

I hope this will help.

Greetings,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Joshua
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or