本文整理汇总了C#中DataView.AddNew方法的典型用法代码示例。如果您正苦于以下问题:C# DataView.AddNew方法的具体用法?C# DataView.AddNew怎么用?C# DataView.AddNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataView
的用法示例。
在下文中一共展示了DataView.AddNew方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddNew
public void AddNew()
{
//create the source datatable
DataTable dt = DataProvider.CreateChildDataTable();
//create the dataview for the table
DataView dv = new DataView(dt);
int CountView = dv.Count;
int CountTable = dt.Rows.Count;
DataRowView drv = null;
// AddNew - DataView Row Count
drv = dv.AddNew();
Assert.Equal(dv.Count, CountView + 1);
// AddNew - Table Row Count
Assert.Equal(dt.Rows.Count, CountTable);
// AddNew - new row in DataTable
drv.EndEdit();
Assert.Equal(dt.Rows.Count, CountTable + 1);
// AddNew - new row != null
Assert.Equal(true, drv != null);
// AddNew - check table
Assert.Equal(dt, drv.Row.Table);
}
示例2: IsNew
public void IsNew()
{
DataTable dt = DataProvider.CreateParentDataTable();
DataView dv = new DataView(dt);
DataRowView drv = dv[0];
// existing row
Assert.Equal(false, drv.IsNew);
// add new row
drv = dv.AddNew();
Assert.Equal(true, drv.IsNew);
}
示例3: RowVersion1
public void RowVersion1()
{
// I guess we could write better tests.
DataTable dt = new DataTable("table");
dt.Columns.Add("col");
dt.Rows.Add(new object[] { 1 });
DataView dv = new DataView(dt);
DataRowView drv = dv.AddNew();
Assert.Equal(DataRowVersion.Current, drv.RowVersion);
Assert.Equal(DataRowVersion.Current, dv[0].RowVersion);
drv["col"] = "mod";
Assert.Equal(DataRowVersion.Current, drv.RowVersion);
Assert.Equal(DataRowVersion.Current, dv[0].RowVersion);
dt.AcceptChanges();
Assert.Equal(DataRowVersion.Current, drv.RowVersion);
Assert.Equal(DataRowVersion.Current, dv[0].RowVersion);
drv.EndEdit();
dv[0].EndEdit();
Assert.Equal(DataRowVersion.Current, drv.RowVersion);
Assert.Equal(DataRowVersion.Current, dv[0].RowVersion);
}
示例4: ItemException
public void ItemException()
{
Assert.Throws<RowNotInTableException>(() =>
{
DataTable dt = new DataTable("table");
dt.Columns.Add("col");
dt.Rows.Add((new object[] { "val" }));
DataView dv = new DataView(dt);
DataRowView drv = dv.AddNew();
drv.Row["col"] = "test";
drv.Row.CancelEdit();
object o = drv["col"];
});
}
示例5: IsEdit
public void IsEdit()
{
DataTable dt = new DataTable("table");
dt.Columns.Add("col");
dt.Rows.Add((new object[] { "val" }));
DataView dv = new DataView(dt);
DataRowView drv = dv[0];
dt.Rows[0].BeginEdit();
Assert.Equal(true, drv.IsEdit);
drv = dv.AddNew();
drv.Row["col"] = "test";
drv.Row.CancelEdit();
Assert.Equal(false, drv.IsEdit);
}
示例6: ListChanged
public void ListChanged()
{
DataTable dt = DataProvider.CreateParentDataTable();
DataView dv = new DataView(dt);
//add event handler
dv.ListChanged += new ListChangedEventHandler(dv_ListChanged);
// ----- Change Value ---------
_evProp = null;
// change value - Event raised
dv[1]["String1"] = "something";
Assert.Equal(true, _evProp != null);
// change value - ListChangedType
Assert.Equal(ListChangedType.ItemChanged, _evProp.lstType);
// change value - NewIndex
Assert.Equal(1, _evProp.NewIndex);
// change value - OldIndex
Assert.Equal(1, _evProp.OldIndex);
// ----- Add New ---------
_evProp = null;
// Add New - Event raised
dv.AddNew();
Assert.Equal(true, _evProp != null);
// Add New - ListChangedType
Assert.Equal(ListChangedType.ItemAdded, _evProp.lstType);
// Add New - NewIndex
Assert.Equal(6, _evProp.NewIndex);
// Add New - OldIndex
Assert.Equal(-1, _evProp.OldIndex);
// ----- Sort ---------
_evProp = null;
// sort - Event raised
dv.Sort = "ParentId Desc";
Assert.Equal(true, _evProp != null);
// sort - ListChangedType
Assert.Equal(ListChangedType.Reset, _evProp.lstType);
// sort - NewIndex
Assert.Equal(-1, _evProp.NewIndex);
// sort - OldIndex
Assert.Equal(-1, _evProp.OldIndex);
//ListChangedType - this was not checked
//Move
//PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema.
//PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema.
//PropertyDescriptorDeleted
}
示例7: AllowNew
public void AllowNew()
{
DataTable dt = DataProvider.CreateParentDataTable();
DataView dv = new DataView(dt);
// AllowNew - default value
Assert.Equal(true, dv.AllowNew);
// AllowNew - true
dv.AllowNew = true;
Assert.Equal(true, dv.AllowNew);
// AllowNew - false
dv.AllowNew = false;
Assert.Equal(false, dv.AllowNew);
// AllowNew - exception
Assert.Throws<DataException>(() =>
{
dv.AddNew();
});
dv.AllowNew = true;
int RowsCount = dv.Count;
// AllowNew - exception
dv.AddNew();
Assert.Equal(RowsCount + 1, dv.Count);
}
示例8: CancelEditAndEvents
public void CancelEditAndEvents()
{
string reference = " =====ItemAdded:3 ------4 =====ItemAdded:3 =====ItemAdded:4 ------5 =====ItemAdded:4 =====ItemAdded:5 ------6 =====ItemDeleted:5 ------5 =====ItemAdded:5";
_eventWriter = new StringWriter();
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Rows.Add(new object[] { 1, 2, 3 });
dt.Rows.Add(new object[] { 1, 2, 3 });
dt.Rows.Add(new object[] { 1, 2, 3 });
DataView dv = new DataView(dt);
dv.ListChanged += new ListChangedEventHandler(ListChanged);
DataRowView a1 = dv.AddNew();
_eventWriter.Write(" ------" + dv.Count);
// I wonder why but MS fires another event here.
a1 = dv.AddNew();
_eventWriter.Write(" ------" + dv.Count);
// I wonder why but MS fires another event here.
DataRowView a2 = dv.AddNew();
_eventWriter.Write(" ------" + dv.Count);
a2.CancelEdit();
_eventWriter.Write(" ------" + dv.Count);
DataRowView a3 = dv.AddNew();
Assert.Equal(reference, _eventWriter.ToString());
}
示例9: Bug18898
public void Bug18898()
{
var table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("1", "2");
table.Rows.Add("4", "3");
table.AcceptChanges();
table.Rows.Add("5", "6");
DataView dv = new DataView(table, string.Empty, string.Empty, DataViewRowState.Added);
dv.AllowNew = true;
var new_row = dv.AddNew();
new_row[0] = "7";
new_row[1] = "8";
var another_new_row = dv.AddNew();
another_new_row[0] = "9";
another_new_row[1] = "10";
Assert.Equal(dv[2][0], "9");
//This should not throw a System.Data.VersionNotFoundException: "There is no Proposed data to accces"
Assert.Equal(dv[1][0], "7");
}