DataTable internal index is corrupted

前端 未结 18 1459
轻奢々
轻奢々 2020-12-04 15:55

I am working with a .NET WinForms app in C#, running against the 3.5 .NET framework. In this app, I am setting the .Expression member of a DataColumn in a

18条回答
  •  庸人自扰
    2020-12-04 16:33

    Same prob over here, and tried a different approach. I'm not using the datatable for any screen related stuff (e.g. binding); I'm just creating DataRow objects (in multiple threads) and adding them to the table.

    I've tried using lock(), and also tried centralizing the addition of the rows into a singleton, thinking this would help. It didn't. For reference, here's the singleton I used. Maybe someone else will be able to build on this and figure something out?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace EntityToDataSet
    {
       public class RowAdder
       {
          #region Data
          private readonly object mLockObject = new object();
          private static RowAdder mInstance;
    
          public static RowAdder Instance
          {
             get
             {
                if (mInstance == null)
                {
                   mInstance = new RowAdder();
                }
                return mInstance;
             }
          }
    
          object mSync;
          #endregion
    
          #region Constructor
          private RowAdder()
          {
          }
          #endregion
    
          public void Add(DataTable table, DataRow row)
          {
             lock (mLockObject)
             {
                table.Rows.Add(row);
             }
          }
       }
    }
    

提交回复
热议问题