WPF C#DataGrid编辑单元格

时间:2013-08-31 15:25:32

标签: c# wpf xaml datagrid

我在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() 
    });

1 个答案:

答案 0 :(得分:2)

创建定义了所有属性的类,并将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;

这可以解决你的问题。

由于