WPF C#DataGrid编辑单元格

本文关键字:单元格 编辑 C#DataGrid WPF | 更新日期: 2023-09-27 18:29:41

我在DataGrid中编辑单元格时遇到问题。

当我双击单元格时,会引发一个异常-InvalidOperationException 'EditItem' is not allowed for this view

我正在寻找在数据网格中编辑单元格的最简单方法。

XAML--------------------------------------------------------------------------------XAML

<DataGrid x:Name="ZdarzeniaDataGrid" AutoGenerateColumns="False" CanUserAddRows="True" HorizontalAlignment="Left" Margin="71,398,0,0" VerticalAlignment="Top" Height="279" Width="1119">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding IdZdarzenia}" Header="Id Zdarzenia" Width="100"/>
                <DataGridTextColumn Binding="{Binding Gospodarze}"  Header="Gospodarze" Width="100"/>
                <DataGridTextColumn Binding="{Binding Goście}"  Header="Goście" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs1}" Header="Kurs 1" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs0}" Header="Kurs 0" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs2}" Header="Kurs 2" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs10}"  Header="Kurs 10" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs20}" Header="Kurs 20" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs12}" Header="Kurs 12" Width="100"/>
                <DataGridTextColumn Binding="{Binding DataZdarzenia}" Header="Data Zdarzenia" Width="100"/>
             </DataGrid.Columns>
        </DataGrid>

C#-----------------------------------------------------------------------------C#

ZdarzeniaDataGrid.Items.Add(
    new { IdZdarzenia = napis.ItemArray.GetValue(0).ToString(),
          Gospodarze = napis2.ItemArray.GetValue(0).ToString(), 
          Goście = napis3.ItemArray.GetValue(0).ToString(), 
          Kurs1 = napis.ItemArray.GetValue(3).ToString(), 
          Kurs0 = napis.ItemArray.GetValue(4).ToString(), 
          Kurs2 = napis.ItemArray.GetValue(5).ToString(), 
          Kurs10 = napis.ItemArray.GetValue(6).ToString(), 
          Kurs20 = napis.ItemArray.GetValue(7).ToString(), 
          Kurs12 = napis.ItemArray.GetValue(8).ToString(), 
          DataZdarzenia = napis.ItemArray.GetValue(9).ToString() 
    });

WPF C#DataGrid编辑单元格

使用定义的所有属性创建类,并将Datagrid的ItemsSource设置/绑定到Data类的列表。

public class Data
    {
        public string    IdZdarzenia {get;set;}
         public string Gospodarze {get;set;}
        public string Goście {get;set;}
        public string Kurs1 {get;set;}
        public string Kurs0 {get;set;}
        public string Kurs2 {get;set;}
        public string Kurs10 {get;set;}
        public string Kurs20 {get;set;}
        public string Kurs12 {get;set;}
        public string DataZdarzenia { get; set; }
    }

设置ItemSource

    List<Data> dataSource = new List<Data>();
    dataSource.Add(new Data()
    {
                        IdZdarzenia = "a",
                        Gospodarze = "d",
                        Goście = "f",
                        Kurs1 = "g",
                        Kurs0 = "r",
                        Kurs2 = "gs",
                        Kurs10 = "sd",
                        Kurs20 = "sds",
                        Kurs12 = "sdss",
                        DataZdarzenia = "eeee"});
    ZdarzeniaDataGrid.ItemsSource = dataSource;

这应该可以解决你的问题。

感谢