本文整理汇总了C#中DataView.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# DataView.CopyTo方法的具体用法?C# DataView.CopyTo怎么用?C# DataView.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataView
的用法示例。
在下文中一共展示了DataView.CopyTo方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RowFilter
public void RowFilter()
{
//note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
// this test also check DataView.Count property
DataRowView[] drvResult = null;
ArrayList al = new ArrayList();
//create the source datatable
DataTable dt = DataProvider.CreateChildDataTable();
//create the dataview for the table
DataView dv = new DataView(dt);
//-------------------------------------------------------------
//Get excpected result
al.Clear();
foreach (DataRow dr in dt.Rows)
{
if ((int)dr["ChildId"] == 1)
{
al.Add(dr);
}
}
// RowFilter = 'ChildId=1', check count
dv.RowFilter = "ChildId=1";
Assert.Equal(al.Count, dv.Count);
// RowFilter = 'ChildId=1', check rows
drvResult = new DataRowView[dv.Count];
dv.CopyTo(drvResult, 0);
//check that the filterd rows exists
bool Succeed = true;
for (int i = 0; i < drvResult.Length; i++)
{
Succeed = al.Contains(drvResult[i].Row);
if (!Succeed) break;
}
Assert.Equal(true, Succeed);
//-------------------------------------------------------------
//-------------------------------------------------------------
//Get excpected result
al.Clear();
foreach (DataRow dr in dt.Rows)
if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")
al.Add(dr);
// RowFilter - ChildId=1 and String1='1-String1'
dv.RowFilter = "ChildId=1 and String1='1-String1'";
Assert.Equal(al.Count, dv.Count);
// RowFilter = ChildId=1 and String1='1-String1', check rows
drvResult = new DataRowView[dv.Count];
dv.CopyTo(drvResult, 0);
//check that the filterd rows exists
Succeed = true;
for (int i = 0; i < drvResult.Length; i++)
{
Succeed = al.Contains(drvResult[i].Row);
if (!Succeed) break;
}
Assert.Equal(true, Succeed);
//-------------------------------------------------------------
//EvaluateException
// RowFilter - check EvaluateException
Assert.Throws<EvaluateException>(() =>
{
dv.RowFilter = "Col=1";
});
//SyntaxErrorException 1
// RowFilter - check SyntaxErrorException 1
Assert.Throws<SyntaxErrorException>(() =>
{
dv.RowFilter = "sum('something')";
});
//SyntaxErrorException 2
// RowFilter - check SyntaxErrorException 2
Assert.Throws<SyntaxErrorException>(() =>
{
dv.RowFilter = "HH**!";
});
}
示例2: CopyTo
public void CopyTo()
{
//create the source datatable
DataTable dt = DataProvider.CreateChildDataTable();
//create the dataview for the table
DataView dv = new DataView(dt);
DataRowView[] drvExpected = null;
DataRowView[] drvResult = null;
// ------- Copy from Index=0
drvExpected = new DataRowView[dv.Count];
for (int i = 0; i < dv.Count; i++)
{
drvExpected[i] = dv[i];
}
drvResult = new DataRowView[dv.Count];
// CopyTo from index 0
dv.CopyTo(drvResult, 0);
Assert.Equal(drvResult, drvExpected);
// ------- Copy from Index=3
drvExpected = new DataRowView[dv.Count + 3];
for (int i = 0; i < dv.Count; i++)
{
drvExpected[i + 3] = dv[i];
}
drvResult = new DataRowView[dv.Count + 3];
// CopyTo from index 3
dv.CopyTo(drvResult, 3);
Assert.Equal(drvResult, drvExpected);
// ------- Copy from Index=3,larger array
drvExpected = new DataRowView[dv.Count + 9];
for (int i = 0; i < dv.Count; i++)
{
drvExpected[i + 3] = dv[i];
}
drvResult = new DataRowView[dv.Count + 9];
// CopyTo from index 3,larger array
dv.CopyTo(drvResult, 3);
Assert.Equal(drvResult, drvExpected);
// ------- CopyTo smaller array, check exception
drvResult = new DataRowView[dv.Count - 1];
// CopyTo smaller array, check exception
Assert.Throws<IndexOutOfRangeException>(() =>
{
dv.CopyTo(drvResult, 0);
});
}