正在刷新数据网格wpf.ItemsSource和Items.Refresh()不工作



我有一个简单的应用程序,我将数据从excel文件导入数据网格。这是代码:

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

它运行良好。但我想补充一些数据。我使用这个代码:

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

而且它工作得很好。我的数据网格有问题。在我向excel文件中添加一些内容后,它不会更新。

我试着做这样的事情:

dtGrid.ItemsSource = null;
InitializeDataGridView();

它不起作用。我也用了dtGrid.Items.Refresh();,它不起作用。将数据添加到excel文件后,如何刷新数据网格?

InitializeDataGridView()具有数据网格。引用数据表dt的ItemsSource。默认视图。

我没有看到在添加新数据时使用相同的数据表。如果创建新的dataTable并将数据添加到其中,则需要将其重新连接为ItemsSource的新引用。

我建议在显示数据网格的整个会话中使用相同的ItemsSource。

最新更新