system.data.datatable 中的 newrow()方法是怎么实现的?

thinkhejie 2009-08-10 03:25:45
system.data.datatable 中的 newrow()方法是怎么实现的?

internal void InsertRow(DataRow row, int proposedID, int pos, bool fireEvent)
{
Exception deferredException = null;
bool flag;
if (row == null)
{
throw ExceptionBuilder.ArgumentNull("row");
}
if (row.Table != this)
{
throw ExceptionBuilder.RowAlreadyInOtherCollection();
}
if (row.rowID != -1)
{
throw ExceptionBuilder.RowAlreadyInTheCollection();
}
row.BeginEdit();
int tempRecord = row.tempRecord;
row.tempRecord = -1;
if (proposedID == -1)
{
proposedID = this.nextRowID;
}
if (flag = this.nextRowID <= proposedID)
{
this.nextRowID = proposedID + 1;
}
try
{
try
{
row.rowID = proposedID;
this.SetNewRecordWorker(row, tempRecord, DataRowAction.Add, false, pos, fireEvent, out deferredException);
}
catch
{
if (flag && (this.nextRowID == (proposedID + 1)))
{
this.nextRowID = proposedID;
}
row.rowID = -1;
row.tempRecord = tempRecord;
throw;
}
if (deferredException != null)
{
throw deferredException;
}
if (this.EnforceConstraints && !this.inLoad)
{
int count = this.columnCollection.Count;
for (int i = 0; i < count; i++)
{
DataColumn column = this.columnCollection[i];
if (column.Computed)
{
column.CheckColumnConstraint(row, DataRowAction.Add);
}
}
}
}
finally
{
row.ResetLastChangedColumn();
}
}

上面是代码 盼望懂的朋友 来讲讲。
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinkhejie 2009-08-13
  • 打赏
  • 举报
回复
其实 我是看到 datatable 的源码之后 发现 Row 和 column 都是 数组 所以我很好奇 他是怎么动态的添加 行 和 列的,我贴的代码也是datatable的源代码,可惜没人理我。
算了 分就给楼上的2位了。
ds252743641 2009-08-10
  • 打赏
  • 举报
回复
DataTable dt = new DataTable();
dt.Columns.Add("标题名");

DataRow row = dt.NewRow();
row[0] = "121";

dt.Rows.Add(row);
hecker728 2009-08-10
  • 打赏
  • 举报
回复
增加行   
thinkhejie 2009-08-10
  • 打赏
  • 举报
回复
错了 不是newrow()! 是 addrow() 不好意思!

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧