C# DataRow.ItemArray 属性
2014-03-04 14:15
281 查看
DataRow.ItemArray 属性
通过一个数组来获取或设置此行的所有值。
命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)
代码示例:
DataRow.ItemArray 属性源代码实现:
通过一个数组来获取或设置此行的所有值。
命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)
代码示例:
private void CreateRowsWithItemArray() { // Make a DataTable using the function below. DataTable dt = MakeTableWithAutoIncrement(); DataRow relation; // Declare the array variable. object [] rowArray = new object[2]; // Create 10 new rows and add to DataRowCollection. for(int i = 0; i <10; i++) { rowArray[0]=null; rowArray[1]= "item " + i; relation = dt.NewRow(); relation.ItemArray = rowArray; dt.Rows.Add(relation); } PrintTable(dt); } private DataTable MakeTableWithAutoIncrement() { // Make a table with one AutoIncrement column. DataTable table = new DataTable("table"); DataColumn idColumn = new DataColumn("id", Type.GetType("System.Int32")); idColumn.AutoIncrement = true; idColumn.AutoIncrementSeed = 10; table.Columns.Add(idColumn); DataColumn firstNameColumn = new DataColumn("Item", Type.GetType("System.String")); table.Columns.Add(firstNameColumn); return table; } private void PrintTable(DataTable table) { foreach(DataRow row in table.Rows) { foreach(DataColumn column in table.Columns) { Console.WriteLine(row[column]); } } }异常:
异常类型 | 条件 |
---|---|
ArgumentException | 数组大于表中的列数。 |
InvalidCastException | 数组中的值与其相应的 DataColumn 中的 DataType 不匹配。 |
ConstraintException | 编辑破坏了约束。 |
ReadOnlyException | 编辑试图更改只读列的值。 |
NoNullAllowedException | 编辑试图将空值放在 DataColumn 对象的 AllowDBNull 为 false 的列中。 |
DeletedRowInaccessibleException | 该行已被删除。 |
public object[] ItemArray { get { int defaultRecord = this.GetDefaultRecord(); object[] array = new object[this._columns.Count]; for (int i = 0; i < array.Length; i++) { DataColumn dataColumn = this._columns[i]; array[i] = dataColumn[defaultRecord]; } return array; } set { if (value == null) { throw ExceptionBuilder.ArgumentNull("ItemArray"); } if (this._columns.Count < value.Length) { throw ExceptionBuilder.ValueArrayLength(); } DataColumnChangeEventArgs dataColumnChangeEventArgs = null; if (this._table.NeedColumnChangeEvents) { dataColumnChangeEventArgs = new DataColumnChangeEventArgs(this); } bool flag = this.BeginEditInternal(); for (int i = 0; i < value.Length; i++) { if (value[i] != null) { DataColumn dataColumn = this._columns[i]; if (-1L != this.rowID && dataColumn.ReadOnly) { throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName); } if (dataColumnChangeEventArgs != null) { dataColumnChangeEventArgs.InitializeColumnChangeEvent(dataColumn, value[i]); this._table.OnColumnChanging(dataColumnChangeEventArgs); } if (dataColumn.Table != this._table) { throw ExceptionBuilder.ColumnNotInTheTable(dataColumn.ColumnName, this._table.TableName); } if (-1L != this.rowID && dataColumn.ReadOnly) { throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName); } if (this.tempRecord == -1) { this.BeginEditInternal(); } object obj = (dataColumnChangeEventArgs != null) ? dataColumnChangeEventArgs.ProposedValue : value[i]; if (obj == null) { if (dataColumn.IsValueType) { throw ExceptionBuilder.CannotSetToNull(dataColumn); } obj = DBNull.Value; } try { int proposedRecordNo = this.GetProposedRecordNo(); dataColumn[proposedRecordNo] = obj; } catch (Exception e) { if (ADP.IsCatchableOrSecurityExceptionType(e) && flag) { this.CancelEdit(); } throw; } this.LastChangedColumn = dataColumn; if (dataColumnChangeEventArgs != null) { this._table.OnColumnChanged(dataColumnChangeEventArgs); } } } this.EndEdit(); } }
相关文章推荐
- DataBinder.Eval:“System.Data.DataRowView”不包含名称为 cid 的属性。
- OWC简介及其属性
- C#——在制作第三方控件时设置属性具有存储功能
- SyntaxHighlighter 使用参考(七) – highlight 属性
- jQuery文档分析4-属性的动态设置
- ie6使用了float:属性后,margin的值为双倍距离解决办法
- 关于ArrayList中对象按照属性进行分类(属性相同的对象放入新的arrayList里面)
- C#中常用控件及其属性的用法
- 变量的存储属性与内存管理
- ligerGrid中的detail明细属性
- ajax引擎对象通过responseText属性 或者responseXML属性 接收服务器回送的数据
- UIView 中autoresizingMask的属性
- Spring Data Jpa 自定义属性查询规则
- Meta http-equiv属性详解(转)
- css-background属性
- html5 新增元素属性和废除的元素
- H5 21-属性选择器下
- Android AndroidManifest.xml文件的android:supportsRtl属性详解
- vue中属性动态绑定数据方法
- Dubbo在XML中各配置标签属性含义