简体   繁体   中英

Cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

I have this issue when I tried to add new Rows to my DataGrideView :

public List<PrefModel> GetPerfLog()
{
    try
    {
        List<PerfModelList> objects = File.ReadAllLines($"D:\\PrefLog.json")
        .Select(line => JsonConvert.DeserializeObject<PerfModelList>(line))
        .ToList();
        var perf = from PerfModelList perflist in objects select perflist.FlogDetail;
        
        
        return perf.ToList();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

private void Form1_Load(object sender, EventArgs e)
{


  
    dgvPerformance.DataSource = _readingLogFiles.GetPerfLog();
    
    
}

private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType != WatcherChangeTypes.Changed)
    {
        return;
    }

    dgvPerformance.Rows.Add(_readingLogFiles.GetlastPerfLog());
}

Add to the data source, not the grid

private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType != WatcherChangeTypes.Changed)
    {
        return;
    }

    var x =  dgvperformance.DataSource as List<PrefLog>;      
    x.Add(_readingLogFiles.GetlastPerfLog());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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