本文整理汇总了C#中DataTable.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.Clear方法的具体用法?C# DataTable.Clear怎么用?C# DataTable.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.Clear方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ClearReset
public void ClearReset() //To test Clear and Reset methods
{
DataTable table = new DataTable("table");
DataTable table1 = new DataTable("table1");
DataSet set = new DataSet();
set.Tables.Add(table);
set.Tables.Add(table1);
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Constraints.Add(new UniqueConstraint("UK1", table.Columns[0]));
table.CaseSensitive = false;
table1.Columns.Add("Id", typeof(int));
table1.Columns.Add("Name", typeof(string));
DataRelation dr = new DataRelation("DR", table.Columns[0], table1.Columns[0]);
set.Relations.Add(dr);
DataRow row = table.NewRow();
row["Id"] = 147;
row["name"] = "Roopa";
table.Rows.Add(row);
row = table.NewRow();
row["Id"] = 47;
row["Name"] = "roopa";
table.Rows.Add(row);
Assert.Equal(2, table.Rows.Count);
Assert.Equal(1, table.ChildRelations.Count);
try
{
table.Reset();
Assert.False(true);
}
catch (ArgumentException)
{
}
Assert.Equal(0, table.Rows.Count);
Assert.Equal(0, table.ChildRelations.Count);
Assert.Equal(0, table.ParentRelations.Count);
Assert.Equal(0, table.Constraints.Count);
table1.Reset();
Assert.Equal(0, table1.Rows.Count);
Assert.Equal(0, table1.Constraints.Count);
Assert.Equal(0, table1.ParentRelations.Count);
// clear test
table.Clear();
Assert.Equal(0, table.Rows.Count);
Assert.Equal(0, table.Constraints.Count);
Assert.Equal(0, table.ChildRelations.Count);
}
示例2: IgnoreSchemaShouldFillData
public void IgnoreSchemaShouldFillData()
{
// no such dataset
string xml1 = "<set><tab><col>test</col></tab></set>";
// no wrapper element
string xml2 = "<tab><col>test</col></tab>";
// no such table
string xml3 = "<tar><col>test</col></tar>";
var ds = new DataSet();
DataTable dt = new DataTable("tab");
ds.Tables.Add(dt);
dt.Columns.Add("col");
ds.ReadXml(new StringReader(xml1), XmlReadMode.IgnoreSchema);
AssertDataSet("ds", ds, "NewDataSet", 1, 0);
Assert.Equal(1, dt.Rows.Count);
dt.Clear();
ds.ReadXml(new StringReader(xml2), XmlReadMode.IgnoreSchema);
Assert.Equal(1, dt.Rows.Count);
dt.Clear();
ds.ReadXml(new StringReader(xml3), XmlReadMode.IgnoreSchema);
Assert.Equal(0, dt.Rows.Count);
}
示例3: ClearTable
public void ClearTable()
{
_evProp = null;
DataTable dt = new DataTable();
IBindingList list = dt.DefaultView;
list.ListChanged += new ListChangedEventHandler(dv_ListChanged);
dt.Columns.Add("test", typeof(int));
dt.Rows.Add(new object[] { 10 });
dt.Rows.Add(new object[] { 20 });
// Clears DataTable
dt.Clear();
Assert.Equal(true, _evProp != null);
// Clear DataTable - should emit ListChangedType.Reset
Assert.Equal(ListChangedType.Reset, _evProp.lstType);
// Clear DataTable - should clear view count
Assert.Equal(0, dt.DefaultView.Count);
}
示例4: ClearTest
public void ClearTest()
{
DataTable table = new DataTable("test");
table.Columns.Add("id", typeof(int));
table.Columns.Add("name", typeof(string));
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
table.Rows.Add(new object[] { 1, "mono 1" });
table.Rows.Add(new object[] { 2, "mono 2" });
table.Rows.Add(new object[] { 3, "mono 3" });
table.Rows.Add(new object[] { 4, "mono 4" });
table.AcceptChanges();
_tableClearedEventFired = false;
table.TableCleared += new DataTableClearEventHandler(OnTableCleared);
_tableClearingEventFired = false;
table.TableClearing += new DataTableClearEventHandler(OnTableClearing);
table.Clear();
Assert.True(_tableClearingEventFired);
Assert.True(_tableClearedEventFired);
DataRow r = table.Rows.Find(1);
Assert.True(r == null);
// try adding new row. indexes should have cleared
table.Rows.Add(new object[] { 2, "mono 2" });
Assert.Equal(1, table.Rows.Count);
}