2013-07-05 58 views
0

我有一個表有幾列,我想添加一個沒有給它一個值的列,因爲它是一個(有點)複雜的對象,每次使用該表時可能都沒用。我可以在數據綁定後期創建列值嗎?

所以我當前的代碼看起來是這樣的:

  var com = new SqlConnection(functions.ConnectionString).CreateCommand(); 
      com.CommandText = @" 
SELECT 
    S.id 
    , S.Name 
    , S.MemberID 
FROM dbo.SubSets AS S 
WHERE S.SubsetType = 0 
AND S.MemberId > 1 
"; 
      this.tblSubsets = new DataTable(); 

      new SqlDataAdapter(com).Fill(this.tblSubsets); 

      this.tblSubsets.Columns.Add(new DataColumn() 
      { 
       AllowDBNull = true 
       , ColumnName = "Member" 
       , DataType = typeof(object) // ? 
       , DefaultValue = DBNull.Value // late binding mechanism? 
      }); 

我怎樣才能讓該列Member巋然不動「空」,直到它被使用DoSomethingwith(table.Rows[x]["Member"])

回答

0

我猜你不行。

我結束了存儲Func<T>並自動設置該字段,每當一行被添加到表中。
再加上關閉字典服務的性能目標。

  this.tblSubsets = new DataTable(); 

      this.tblSubsets.Columns.AddRange(new DataColumn[] { 
       new DataColumn() 
       { 
        AllowDBNull = false 
        , ColumnName = "Member" 
        , DataType = typeof(Func<Member>) 
       } 
      }); 

      var members = new Dictionary<int, Member>(); 
      this.tblSubsets.RowChanged += new DataRowChangeEventHandler((object sender, DataRowChangeEventArgs e) => 
      { 
       if (e.Action != DataRowAction.Add) 
       { 
        return; 
       } 

       e.Row["Member"] = new Func<Member>(() => 
       { 
        Member returnValue; 
        var mid = Convert.ToInt32(e.Row["MemberID"]); 

        if (!members.TryGetValue(mid, out returnValue)) 
        { 
         members.Add(mid, (returnValue = new Member(mid))); 
        } 

        return returnValue; 
       }); 
      }); 


      var com = new SqlConnection(functions.ConnectionString).CreateCommand(); 
      com.CommandText = @" 
SELECT 
    S.id 
    , S.Name 
    , S.MemberID 
FROM dbo.SubSets AS S 
WHERE S.SubsetType = 0 
"; 
      new SqlDataAdapter(com).Fill(this.tblSubsets); 
相關問題