本文整理汇总了C#中DataTable.NewRow方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.NewRow方法的具体用法?C# DataTable.NewRow怎么用?C# DataTable.NewRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.NewRow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataViewTest
public DataViewTest()
{
_dataTable = new DataTable("itemTable");
_dc1 = new DataColumn("itemId");
_dc2 = new DataColumn("itemName");
_dc3 = new DataColumn("itemPrice");
_dc4 = new DataColumn("itemCategory");
_dataTable.Columns.Add(_dc1);
_dataTable.Columns.Add(_dc2);
_dataTable.Columns.Add(_dc3);
_dataTable.Columns.Add(_dc4);
DataRow dr;
_seed = 123;
_rowCount = 5;
_rndm = new Random(_seed);
for (int i = 1; i <= _rowCount; i++)
{
dr = _dataTable.NewRow();
dr["itemId"] = "item " + i;
dr["itemName"] = "name " + _rndm.Next();
dr["itemPrice"] = "Rs. " + (_rndm.Next() % 1000);
dr["itemCategory"] = "Cat " + ((_rndm.Next() % 10) + 1);
_dataTable.Rows.Add(dr);
}
_dataTable.AcceptChanges();
_dataView = new DataView(_dataTable);
_dataView.ListChanged += new ListChangedEventHandler(OnListChanged);
_listChangedArgs = null;
}
示例2: DataRowTest
public DataRowTest()
{
_table = MakeTable();
_row = _table.NewRow();
_row["FName"] = "Hello";
_row["LName"] = "World";
_table.Rows.Add(_row);
}
示例3: AcceptChanges
public void AcceptChanges()
{
DataTable myTable = new DataTable("myTable");
DataRow myRow;
myRow = myTable.NewRow();
myTable.Rows.Add(myRow);
// DataRow AcceptChanges
// DataRowState.Added -> DataRowState.Unchanged
myTable.AcceptChanges();
Assert.Equal(DataRowState.Unchanged, myRow.RowState);
}
示例4: ClearErrors
public void ClearErrors()
{
DataTable dt = new DataTable("myTable");
DataRow dr = dt.NewRow();
dr.RowError = "err";
// DataRow ClearErrors
Assert.Equal(true, dr.HasErrors);
// DataRow ClearErrors
dr.ClearErrors();
Assert.Equal(false, dr.HasErrors);
}
示例5: AllowDBNull
public void AllowDBNull()
{
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("ColName", typeof(int));
dc.DefaultValue = DBNull.Value;
dt.Columns.Add(dc);
dc.AutoIncrement = false;
// Checking default value (True)
Assert.Equal(true, dc.AllowDBNull);
// AllowDBNull=true - adding new row with null value
dt.Rows.Add(dt.NewRow());
Assert.Equal(DBNull.Value, dt.Rows[0][0]);
// set AllowDBNull=false
Assert.Throws<DataException>(() =>
{
dc.AllowDBNull = false; //the exisiting row contains null value
});
dt.Rows.Clear();
dc.AllowDBNull = false;
// AllowDBNull=false - adding new row with null value
Assert.Throws<NoNullAllowedException>(() =>
{
dt.Rows.Add(dt.NewRow());
});
dc.AutoIncrement = true;
int iRowCount = dt.Rows.Count;
// AllowDBNull=false,AutoIncrement=true - adding new row with null value
dt.Rows.Add(dt.NewRow());
Assert.Equal(dt.Rows.Count, iRowCount + 1);
}
示例6: FillDataTable
// bindAll = true => bind all property except "Items" event <TreeListObjects> has no child items
public int FillDataTable(DataTable table, Hashtable values, bool bindAll)
{
object obj;
bool rootObj = false, // this is the first objectList (used to assign to GridView)
haveDataToBind = false;
Type t = this.GetType();
PropertyInfo[] props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// get current level values
if (values == null) { values = new Hashtable(); rootObj = true; }
foreach (PropertyInfo prop in props)
{
if (prop.Name == "Items") continue; // bypass children
obj = prop.GetValue(this, null);
if ((obj != null) && (table.Columns.Contains(prop.Name)))
{
values[prop.Name] = obj;
if (obj != null) haveDataToBind = true;
}
}
// get child level values
RowSpan = 0;
if (Items.Count > 0)
foreach (TreeListObject child in this.Items)
{
RowSpan += child.FillDataTable(table, values, bindAll);
}
else if ((bindAll && (!rootObj)) || // do not fill root obj when it has no child => show empty Grid
haveDataToBind) // is there any data in current level to bind?
{
DataRow row = table.NewRow();
foreach (DictionaryEntry val in values) { row[val.Key.ToString()] = val.Value; }
table.Rows.Add(row);
RowSpan = 1;
}
// clear current level values
foreach (PropertyInfo prop in props)
{
if ((prop.Name != "Items") && (values.Contains(prop.Name))) values.Remove(prop.Name);
}
// row span for this level
return RowSpan;
}
示例7: BuildDataReader
public IDataReader BuildDataReader()
{
var authorizationSetDataTable = new DataTable();
authorizationSetDataTable.Columns.Add(FieldName.SmallInt, typeof(Int16));
authorizationSetDataTable.Columns.Add(FieldName.Int, typeof(Int32));
authorizationSetDataTable.Columns.Add(FieldName.BigInt, typeof(Int64));
authorizationSetDataTable.Columns.Add(FieldName.Guid, typeof(Guid));
authorizationSetDataTable.Columns.Add(FieldName.Float, typeof(float));
authorizationSetDataTable.Columns.Add(FieldName.Double, typeof(Double));
authorizationSetDataTable.Columns.Add(FieldName.Decimal, typeof(Decimal));
authorizationSetDataTable.Columns.Add(FieldName.DateTime, typeof(DateTime));
authorizationSetDataTable.Columns.Add(FieldName.Byte, typeof(Byte));
authorizationSetDataTable.Columns.Add(FieldName.Boolean, typeof(Boolean));
authorizationSetDataTable.Columns.Add(FieldName.Something, typeof(DateTime));
var authorizationSetDataRow = authorizationSetDataTable.NewRow();
authorizationSetDataRow[FieldName.SmallInt] = 22;
authorizationSetDataRow[FieldName.Int] = 6134;
authorizationSetDataRow[FieldName.BigInt] = 61346154;
authorizationSetDataRow[FieldName.Guid] = Guid.NewGuid();
authorizationSetDataRow[FieldName.Float] = 642.61;
authorizationSetDataRow[FieldName.Double] = 67164.64;
authorizationSetDataRow[FieldName.Decimal] = 94341.61;
authorizationSetDataRow[FieldName.DateTime] = DateTime.Now;
authorizationSetDataRow[FieldName.Byte] = 0x12;
authorizationSetDataRow[FieldName.Boolean] = true;
authorizationSetDataRow[FieldName.Something] = DateTime.MaxValue;
authorizationSetDataTable.Rows.Add(authorizationSetDataRow);
return authorizationSetDataTable.CreateDataReader();
}
示例8: CloneCopy
public void CloneCopy()
{
DataTable table = new DataTable("pTable");
DataTable table1 = new DataTable("cTable");
DataSet set = new DataSet();
set.Tables.Add(table);
set.Tables.Add(table1);
DataColumn col = new DataColumn();
col.ColumnName = "Id";
col.DataType = Type.GetType("System.Int32");
table.Columns.Add(col);
UniqueConstraint uc = new UniqueConstraint("UK1", table.Columns[0]);
table.Constraints.Add(uc);
col = new DataColumn();
col.ColumnName = "Name";
col.DataType = Type.GetType("System.String");
table.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "Id";
col.DataType = Type.GetType("System.Int32");
table1.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "Name";
col.DataType = Type.GetType("System.String");
table1.Columns.Add(col);
ForeignKeyConstraint fc = new ForeignKeyConstraint("FK1", table.Columns[0], table1.Columns[0]);
table1.Constraints.Add(fc);
DataRow row = table.NewRow();
row["Id"] = 147;
row["name"] = "Row1";
row.RowError = "Error#1";
table.Rows.Add(row);
// Set column to RO as commonly used by auto-increment fields.
// ds.Copy() has to omit the RO check when cloning DataRows
table.Columns["Id"].ReadOnly = true;
row = table1.NewRow();
row["Id"] = 147;
row["Name"] = "Row1";
table1.Rows.Add(row);
//Setting properties of DataSet
set.CaseSensitive = true;
set.DataSetName = "My DataSet";
set.EnforceConstraints = false;
set.Namespace = "Namespace#1";
set.Prefix = "Prefix:1";
DataRelation dr = new DataRelation("DR", table.Columns[0], table1.Columns[0]);
set.Relations.Add(dr);
set.ExtendedProperties.Add("TimeStamp", DateTime.Now);
CultureInfo cultureInfo = new CultureInfo("ar-SA");
set.Locale = cultureInfo;
//Testing Copy ()
DataSet copySet = set.Copy();
Assert.Equal(set.CaseSensitive, copySet.CaseSensitive);
Assert.Equal(set.DataSetName, copySet.DataSetName);
Assert.Equal(set.EnforceConstraints, copySet.EnforceConstraints);
Assert.Equal(set.HasErrors, copySet.HasErrors);
Assert.Equal(set.Namespace, copySet.Namespace);
Assert.Equal(set.Prefix, copySet.Prefix);
Assert.Equal(set.Relations.Count, copySet.Relations.Count);
Assert.Equal(set.Tables.Count, copySet.Tables.Count);
Assert.Equal(set.ExtendedProperties["TimeStamp"], copySet.ExtendedProperties["TimeStamp"]);
for (int i = 0; i < copySet.Tables.Count; i++)
{
Assert.Equal(set.Tables[i].Rows.Count, copySet.Tables[i].Rows.Count);
Assert.Equal(set.Tables[i].Columns.Count, copySet.Tables[i].Columns.Count);
}
//Testing Clone ()
copySet = set.Clone();
Assert.Equal(set.CaseSensitive, copySet.CaseSensitive);
Assert.Equal(set.DataSetName, copySet.DataSetName);
Assert.Equal(set.EnforceConstraints, copySet.EnforceConstraints);
Assert.False(copySet.HasErrors);
Assert.Equal(set.Namespace, copySet.Namespace);
Assert.Equal(set.Prefix, copySet.Prefix);
Assert.Equal(set.Relations.Count, copySet.Relations.Count);
Assert.Equal(set.Tables.Count, copySet.Tables.Count);
Assert.Equal(set.ExtendedProperties["TimeStamp"], copySet.ExtendedProperties["TimeStamp"]);
for (int i = 0; i < copySet.Tables.Count; i++)
{
Assert.Equal(0, copySet.Tables[i].Rows.Count);
Assert.Equal(set.Tables[i].Columns.Count, copySet.Tables[i].Columns.Count);
}
}
示例9: WriteXmlModeSchema
//.........这里部分代码省略.........
</xs:keyref>
</xs:element>
</xs:schema>
<Dimension>
<Number>0</Number>
</Dimension>
<Dimension>
<Number>1</Number>
</Dimension>
<Element>
<Dimension>0</Dimension>
<Number>0</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>1</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>2</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>3</Number>
</Element>
<Element>
<Dimension>1</Dimension>
<Number>0</Number>
</Element>
<Element>
<Dimension>1</Dimension>
<Number>1</Number>
</Element>
</Example>";
DataSet ds = new DataSet("Example");
// Dimension DataTable
DataTable dt1 = new DataTable("Dimension");
ds.Tables.Add(dt1);
dt1.Columns.Add(new DataColumn("Number", typeof(int)));
dt1.Columns["Number"].AllowDBNull = false;
dt1.Constraints.Add("PK_Dimension", dt1.Columns["Number"], true);
// Element DataTable
DataTable dt2 = new DataTable("Element");
ds.Tables.Add(dt2);
dt2.Columns.Add(new DataColumn("Dimension", typeof(int)));
dt2.Columns["Dimension"].AllowDBNull = false;
dt2.Columns.Add(new DataColumn("Number", typeof(int)));
dt2.Columns["Number"].AllowDBNull = false;
dt2.Constraints.Add("PK_Element", new DataColumn[] {
dt2.Columns ["Dimension"],
dt2.Columns ["Number"] },
true);
// Add DataRelations
ds.Relations.Add("FK_Element_To_Dimension",
dt1.Columns["Number"],
dt2.Columns["Dimension"], true);
// Add 2 Dimensions
for (int i = 0; i < 2; i++)
{
DataRow newRow = dt1.NewRow();
newRow["Number"] = i;
dt1.Rows.Add(newRow);
}
// Dimension 0 => 4 Elements
for (int i = 0; i < 4; i++)
{
DataRow newRow = dt2.NewRow();
newRow["Dimension"] = 0;
newRow["Number"] = i;
dt2.Rows.Add(newRow);
}
// Dimension 1 => 2 Elements
for (int i = 0; i < 2; i++)
{
DataRow newRow = dt2.NewRow();
newRow["Dimension"] = 1;
newRow["Number"] = i;
dt2.Rows.Add(newRow);
}
ds.AcceptChanges();
StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.WriteSchema);
string result = sw.ToString();
Assert.Equal(result.Replace("\r\n", "\n"), xml.Replace("\r\n", "\n"));
}
示例10: Test_Indexes
public void Test_Indexes()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("A");
dt.Columns.Add(dc);
dc = new DataColumn("B");
dt.Columns.Add(dc);
dc = new DataColumn("C");
dt.Columns.Add(dc);
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["A"] = i;
dr["B"] = i + 1;
dr["C"] = i + 2;
dt.Rows.Add(dr);
}
DataRow[] rows = dt.Select("A=5");
Assert.Equal(1, rows.Length);
dt.Columns.Remove("A");
dc = new DataColumn("A");
dc.DefaultValue = 5;
dt.Columns.Add(dc);
rows = dt.Select("A=5");
Assert.Equal(10, rows.Length);
}
示例11: MakeParentTable1
private void MakeParentTable1()
{
// Create a new Table
_parentTable1 = new DataTable("ParentTable");
_dataSet = new DataSet("XmlDataSet");
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to Table.
column = new DataColumn();
column.DataType = typeof(int);
column.ColumnName = "id";
column.Unique = true;
// Add the Column to the DataColumnCollection.
_parentTable1.Columns.Add(column);
// Create second column
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.Unique = false;
// Add the column to the table
_parentTable1.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = typeof(int);
column.ColumnName = "DepartmentID";
column.Caption = "DepartmentID";
// Add the column to the table.
_parentTable1.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[2];
PrimaryKeyColumns[0] = _parentTable1.Columns["id"];
PrimaryKeyColumns[1] = _parentTable1.Columns["DepartmentID"];
_parentTable1.PrimaryKey = PrimaryKeyColumns;
_dataSet.Tables.Add(_parentTable1);
// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i <= 2; i++)
{
row = _parentTable1.NewRow();
row["id"] = i + 1;
row["ParentItem"] = "ParentItem " + (i + 1);
row["DepartmentID"] = i + 1;
_parentTable1.Rows.Add(row);
}
}
示例12: MakeDummyTable
private void MakeDummyTable()
{
// Create a new Table
_dataSet = new DataSet();
_dummyTable = new DataTable("DummyTable");
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to Table.
column = new DataColumn();
column.DataType = typeof(int);
column.ColumnName = "id";
column.Unique = true;
// Add the Column to the DataColumnCollection.
_dummyTable.Columns.Add(column);
// Create second column
column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = "DummyItem";
column.AutoIncrement = false;
column.Caption = "DummyItem";
column.Unique = false;
// Add the column to the table
_dummyTable.Columns.Add(column);
_dataSet.Tables.Add(_dummyTable);
// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i <= 2; i++)
{
row = _dummyTable.NewRow();
row["id"] = i + 1;
row["DummyItem"] = "DummyItem " + (i + 1);
_dummyTable.Rows.Add(row);
}
DataRow row1 = _dummyTable.Rows[1];
_dummyTable.AcceptChanges();
row1.BeginEdit();
row1[1] = "Changed_DummyItem " + 2;
row1.EndEdit();
}
示例13: HasVersion_ByDataRowVersion
public void HasVersion_ByDataRowVersion()
{
DataTable t = new DataTable("atable");
t.Columns.Add("id", typeof(int));
t.Columns.Add("name", typeof(string));
t.Columns[0].DefaultValue = 1;
t.Columns[1].DefaultValue = "something";
// row r is detached
DataRow r = t.NewRow();
// HasVersion Test #10
Assert.Equal(false, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #11
Assert.Equal(false, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #12
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #13
Assert.Equal(true, r.HasVersion(DataRowVersion.Proposed));
r[0] = 4;
r[1] = "four";
// HasVersion Test #20
Assert.Equal(false, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #21
Assert.Equal(false, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #22
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #23
Assert.Equal(true, r.HasVersion(DataRowVersion.Proposed));
t.Rows.Add(r);
// now it is "added"
// HasVersion Test #30
Assert.Equal(true, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #31
Assert.Equal(false, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #32
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #33
Assert.Equal(false, r.HasVersion(DataRowVersion.Proposed));
t.AcceptChanges();
// now it is "unchanged"
// HasVersion Test #40
Assert.Equal(true, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #41
Assert.Equal(true, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #42
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #43
Assert.Equal(false, r.HasVersion(DataRowVersion.Proposed));
r.BeginEdit();
r[1] = "newvalue";
// HasVersion Test #50
Assert.Equal(true, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #51
Assert.Equal(true, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #52
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #53
Assert.Equal(true, r.HasVersion(DataRowVersion.Proposed));
r.EndEdit();
// now it is "modified"
// HasVersion Test #60
Assert.Equal(true, r.HasVersion(DataRowVersion.Current));
// HasVersion Test #61
Assert.Equal(true, r.HasVersion(DataRowVersion.Original));
// HasVersion Test #62
Assert.Equal(true, r.HasVersion(DataRowVersion.Default));
// HasVersion Test #63
Assert.Equal(false, r.HasVersion(DataRowVersion.Proposed));
// this or t.AcceptChanges
r.AcceptChanges();
// now it is "unchanged" again
//.........这里部分代码省略.........
示例14: HasErrorsWithNullError
public void HasErrorsWithNullError()
{
DataTable dt = new DataTable("myTable");
DataRow dr = dt.NewRow();
// HasErrors (default)
Assert.Equal(false, dr.HasErrors);
dr.RowError = null;
// HasErrors (set/get)
Assert.Equal(string.Empty, dr.RowError);
Assert.Equal(false, dr.HasErrors);
}
示例15: CreateRow
DataRow CreateRow(DataTable T, DataRow R)
{
//создаем новую запись в таблице
DataRow NR = T.NewRow();
//уникальный номер справочника
NR["IDS"] = Convert.ToInt32(R["IDS"]);
//SNAME - название справочника
NR["SNAME"] = CutChar(R["SNAME"].ToString());
//FNAME (SSQLD)- Имя файла справочника
NR["FNAME"] = ShrinkSQL(R["SSQLD"].ToString());
//FUPDATE (SFDBF) имя файла для обновления справочника
NR["FUPDATE"] = R["SFDBF"].ToString();
return NR;
}