【问题标题】:DataGrid replaces first row instead of adding a new row,DataGrid 替换第一行而不是添加新行,
【发布时间】:2014-04-13 13:11:28
【问题描述】:

我的 WPF 中有一个 Datagrid,通过双击另一个 DataGrid 上的 ObservableCollection 填充了一个 DataGrid(用户单击首选标题和详细信息 DataGrid 填充了相关数据)。

每次我搜索新标题并进行选择时,详细信息 DataGrid 都会替换已经存在的第一行数据。如何使 DataGrid 添加一个新行,以便我可以一次记录多个实体以进行导出?

我的代码如下:

数据网格.cs

// This action will seach the IMDb API for the associated infromation for the IMDBID that is tagged with the title you chose in the ListBox.
   private void Movie_List_MouseDoubleClick(object sender, RoutedEventArgs e)      

     {  
        // Grabs the IMDBID associated to the movie title selected to be used with the second API request.
       // DataRowView row = (DataRowView)Movie_List.SelectedItems;


        string titleID = ((searchInfo)Movie_List.SelectedItem).imdbID;
        string newurl = "http://www.omdbapi.com/?i=" + titleID + "&r=XML";

         // Prepares 2nd API URL request to get data for chosen title.
         // Creates a XML Document  to store the xml data that was sent back by the API.
        var doc = XElement.Load(newurl);

         // Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
        IEnumerable<XElement> movieList = doc.Descendants("movie");
         // Creats an ObservableCollection for the Object Retrievalinfo to be displayed in the Movie_Datagrid, that can be edited.
        ObservableCollection<Retrievalinfo> gridInfo = new ObservableCollection<Retrievalinfo>(movieList.Select(movieElement =>
            new Retrievalinfo()
        {
            title = movieElement.Attribute("title").Value,
            actors = movieElement.Attribute("actors").Value.Split(',').ToList(),
            genre = movieElement.Attribute("genre").Value,
            rated = movieElement.Attribute("rated").Value,
            imdbRating = movieElement.Attribute("imdbRating").Value,
            released = movieElement.Attribute("released").Value,
            runtime = movieElement.Attribute("runtime").Value,
        }));
        Movie_DataGrid.ItemsSource = gridInfo;       
    }

Retrievalinfo 类(用于 ObservableCollection 的对象)

public class Retrievalinfo
{
    public Retrievalinfo()
    {
        actors = new List<string>();
    }

    //Creating a list of info objects that will store all returned data for selected title.
    public string title { get; set; }
    public List<string> actors { get; set; }
    public string genre { get; set; }
    public string rated { get; set; }
    public string imdbRating { get; set; }
    public string released { get; set; }
    public string runtime { get; set; }
    }
}

DataGird XAML(Movie_DataGrid,保存所选电影的详细信息)

<DataGrid x:Name="Movie_DataGrid"
            Grid.ColumnSpan="2"
            HorizontalAlignment="Left"
            Margin="292,107,0,0"
            VerticalAlignment="Top"
            Height="214"
            Width="673"
            AutoGenerateColumns="False"
            ItemsSource="{Binding gridInfo}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Title" Binding="{Binding Path=title}"/>
                    <DataGridTextColumn Header="Main Actor 1" Binding="{Binding Path=actors[0]}"/>
                    <DataGridTextColumn Header="Main Actor 2" Binding="{Binding Path=actors[1]}"/>
                    <DataGridTextColumn Header="Main Actor 3" Binding="{Binding Path=actors[2]}"/>
                    <DataGridComboBoxColumn Header="Digital Format" ItemsSource="{Binding digital}"/>
                    <DataGridComboBoxColumn Header="Physical Format" ItemsSource="{Binding physical}"/>
                    <DataGridTextColumn Header="Genre" Binding="{Binding Path=genre}"/>
                    <DataGridTextColumn Header="Rated" Binding="{Binding Path=rated}"/>
                    <DataGridTextColumn Header="IMDB Rating" Binding="{Binding Path=imdbRating}"/>
                    <DataGridTextColumn Header="Released" Binding="{Binding Path=released}"/>
                    <DataGridTextColumn Header="Runtime" Binding="{Binding Path=runtime}"/>
                    <DataGridComboBoxColumn Header="File Type" ItemsSource="{Binding file_type}"/>
            </DataGrid.Columns>
        </DataGrid>

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    在绑定 Datagrid 之前,您应该将现有项目添加到 List 并将新 Item 添加到 List,然后绑定到 Datagrid。

     List<Retrievalinfo> gridInfo = (List<Retrievalinfo>)Movie_DataGrid.ItemsSource;
    

    将新项目添加到此列表中,

    gridInfo.Add(new Retrievalinfo());
    

    然后将列表绑定到数据网格,

     Movie_DataGrid.ItemsSource = gridInfo;    
    

    【讨论】:

    • mainGridITMDataGrid 无法识别。我找不到任何东西来解决这个问题
    • 请再次检查答案!
    • 哈哈是的意思是评论说我想通了。谢谢
    • 这个答案对你有帮助吗?
    • 使用此代码时,运行时出现错误提示无法转换类型为“System.Collections.ObjectModel.ObservableCollection1[WpfApplication3.Retrievalinfo]' to type 'System.Collections.Generic.List1[WpfApplication3.Retrievalinfo]”的对象。
    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 2023-04-05
    • 2016-05-20
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多