【问题标题】:When clicking on datagrid row it throws me exception - WPF C#单击数据网格行时,它会抛出异常-WPF C#
【发布时间】:2015-04-27 22:41:05
【问题描述】:

每当我尝试双击我的数据网格行来编辑它时,我都会抛出一些对我没有任何意义的异常。悬停如果我将整个数据网格设置为 IsReadOnly 为 true,我希望有问题,但我需要可编辑的第二列和第三列。

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"});

例外

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)

【问题讨论】:

  • 可能重复:this

标签: c# wpf datagrid


【解决方案1】:

您应该使用 ObservableCollection&lt;DataClients&gt;() 作为 Datagrid 的源,而不是将项目直接添加到您的 DataGrid。为此,您创建一个属性ObservableCollection&lt;DataClients&gt; Data 并将其用作DataGrid 中的ItemsSource,就像ItemsSource="{Binding MyBikesOrders}" 一样。然后您可以将项目添加到ObservableCollection,更新DataGrid,您应该可以直接在其中编辑条目。

Here 的问题与您的问题非常相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多