简体   繁体   English

刷新数据网格WPF。 ItemsSource和Items.Refresh()不起作用

[英]Refreshing data grid wpf. ItemsSource and Items.Refresh() not working

I have simple application and I import data from excel file to data grid. 我有简单的应用程序,我将数据从excel文件导入到数据网格。 Here is code: 这是代码:

private void InitializeDataGridView()
    {
        //Open the Excel file using ClosedXML.
        using (XLWorkbook workBook = new XLWorkbook(ExcelFile.ExcelFilePath))
        {
            //Read the first Sheet from Excel file.
            IXLWorksheet workSheet = workBook.Worksheet(1);

            //Create a new DataTable.
            DataTable dt = new DataTable();

            //Loop through the Worksheet rows.
            bool firstRow = true;
            foreach (IXLRow row in workSheet.Rows())
            {
                //Use the first row to add columns to DataTable.
                if (firstRow)
                {
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Columns.Add(cell.Value.ToString());
                    }
                    firstRow = false;
                }
                else
                {
                    //Add rows to DataTable.
                    dt.Rows.Add();
                    int i = 0;
                    foreach (IXLCell cell in row.Cells())
                    {
                        dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
                        i++;
                    }
                }

                dtGrid.ItemsSource = dt.DefaultView;
            }
        }
    }

It works fine. 工作正常。 But I want to add some data. 但是我想添加一些数据。 Im using this code: 我正在使用此代码:

public static void AddData(DateTime date, int time, string title, string description)
    {
        // Opening excel file        
        using (XLWorkbook workBook = new XLWorkbook(ExcelFile.ExcelFilePath))
        {              
            IXLWorksheet worksheet = workBook.Worksheet("Progress"); // Trying to get the Progress worksheet
            // Getting table from DataTable
            var dataTable = GetTable(date, time, title, description);
            // Searching for last used row and first used column
            var lastRowUsed = worksheet.LastRowUsed().RowNumber();
            var firstColumnUsed = worksheet.FirstColumnUsed().ColumnNumber();
            // Insterting data               
            worksheet.Cell(lastRowUsed + 1, firstColumnUsed).InsertData(dataTable.AsEnumerable());

            // Adjusting content to fit
            worksheet.Columns().AdjustToContents();
            // Saving excel file
            workBook.Save();
        }

    }

    public static DataTable GetTable(DateTime date, int time, string title, string description)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Data", typeof(DateTime));
        table.Columns.Add("Czas(minuty)", typeof(int));
        table.Columns.Add("Tytuł", typeof(string));
        table.Columns.Add("Opis", typeof(string));

        table.Rows.Add(date, time, title, description);
        return table;
    }

And it's work fine. 而且工作正常。 I have problem with my data grid. 我的数据网格有问题。 Its not updating after I add something to the excel file. 我将一些内容添加到excel文件后,它没有更新。

I tried to do something like that: 我试图做这样的事情:

dtGrid.ItemsSource = null;
InitializeDataGridView();

It wasnt working. 它没有工作。 I used also dtGrid.Items.Refresh(); 我还使用了dtGrid.Items.Refresh(); , it wasnt working. ,它没有用。 How can I refresh my data grid after adding data to excel file? 将数据添加到excel文件后,如何刷新数据网格?

InitializeDataGridView() has dataGrid.ItemsSource referencing a dataTable dt.DefaultView. InitializeDataGridView()具有引用dataTable dt.DefaultView的dataGrid.ItemsSource。

I don't see the same datatable being used in adding new data. 我看不到用于添加新数据的同一数据表。 If you create new dataTable and add data to it, It needs to be reconnected as new reference of ItemsSource. 如果创建新的dataTable并向其中添加数据,则需要重新连接它作为ItemsSource的新引用。

I would suggest rather use same ItemsSource throughout the session displaying the dataGrid. 我建议在显示dataGrid的整个会话中使用相同的ItemsSource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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