简体   繁体   中英

When clicking on datagrid row it throws me exception - WPF C#

Whenever I try to double click my datagrid row to edit it, I throws me a few exceptions which doesn't say anything to me. Hovever if I set the whole datagrid to IsReadOnly to true, I want have the problem, but I need the second and third columns editable.

XAML

 <DataGrid x:Name="clientList" HorizontalAlignment="Left" Height="225" Margin="11,126,0,0" VerticalAlignment="Top" Width="349" IsSynchronizedWithCurrentItem="False" AutoGenerateColumns="False" HorizontalGridLinesBrush="#FFB9B9B9" VerticalGridLinesBrush="#FF8B8B8B" GridLinesVisibility="Horizontal" CellStyle="{StaticResource Body_Content_DataGrid_Centering}">
        <DataGrid.Resources>
            <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1" >
                <GradientStop Color="#66240000" Offset="0"/>
                <GradientStop Color="#CC240000" Offset="0.65"/>
            </LinearGradientBrush>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Width="30" Header="Id" IsReadOnly="True" Binding="{Binding Id}"/>
            <DataGridTextColumn Width="100" Header="Company" IsReadOnly="False" Binding="{Binding Company}"/>
            <DataGridTextColumn Width="130" Header="Name, Surname" IsReadOnly="False" Binding="{Binding Name}"/>
            <DataGridTemplateColumn Header="Actions" CellTemplate="{StaticResource myTemplate}"/>
        </DataGrid.Columns> 
    </DataGrid>

C#

clientList.Items.Add(new DataClients { Id = 1, Company = "My Company", Name = "Jane Roe"});

Exceptions

Exception:Thrown: "'EditItem' is not allowed for this view."(System.InvalidOperationException)

Exception:Thrown: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." (System.FormatException)

You should use an ObservableCollection<DataClients>() as source for your Datagrid and not add the items directly to your DataGrid . To do this, you create a property ObservableCollection<DataClients> Data and use it as the ItemsSource in your DataGrid like this ItemsSource="{Binding MyBikesOrders}" . You can then add the items to the ObservableCollection , update the DataGrid and you should be able to edit the entries directly in it.

Here 'sa very similar question to yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM