DataView组件可以在DataSet中实现对数据的过滤和分类。下面是DataView对象常用的属性及方法:
属性
Count
在应用RowFilter和RowStateFilter之后,获取DataView中记录的数量
Item
从指定的表获取一行数据
RowFilter
获取或设置用于筛选在DataView中查看哪些行的条件表达式
RowStateFilter
获取或设置用于DataView中的行状态筛选器
Sort
获取或设置DataView的一个或多个排序列以及排序顺序
Table
获取或设置源DataTable
方法
AddNew
将新行添加到DataView
Delete
删除指定索引位置的行
Find
按指定的排序关键字值在DataView中查找行
FindRows
返回DataRowView对象的数组
DataTable的原始输出格式可以通过DefaultView属性来取得,DefaultView属性本身就是DataView对象;我们可以设定DefaultView的属性来指定DataTable的显示格式。
1.用DataView.Sort属性来排序
例子所使用的数据库名为sango,表为people,部分数据如下图:
示例1:
<%
@ Page Language
=
"
C#
"
%>
<%
@ Import Namespace
=
"
System.Data
"
%>
<%
@ Import Namespace
=
"
System.Data.SqlClient
"
%>
<
html
>
<
body
>
<
Script
language
="C#"
runat
="server"
>
public
void
Page_Load(Object sender,EventArgs e) { string strConn
=
"
server=localhost;database=sango;uid=sa;pwd=;
"
; string strCmd
=
"
SELECT * FROM people
"
; DataSet ds
=
new
DataSet(); SqlDataAdapter cmd
=
new
SqlDataAdapter(strCmd,strConn); cmd.Fill(ds,
"
tempTable
"
); DataTable dt
=
ds.Tables[
"
tempTable
"
]; Response.Write(
"
<font color=red>未排序:</font><br>
"
);
//
循环读取数据
for
(
int
i
=
0
;i
<
dt.Rows.Count;i
++
) { Response.Write(dt.DefaultView[i][" 姓名 " ] + " <br> " );
} Response.Write(
"
<hr noshade><font color=red>排序后:</font><br>
"
);
//
按照姓名(读音首字母顺序)的升序排列
dt.DefaultView.Sort
=
"
姓名 ASC
"
;
for
(
int
i
=
0
;i
<
dt.Rows.Count;i
++
) { Response.Write(dt.DefaultView[i][
"
姓名
"
]
+
"
<br>
"
); } }
</
script
>
</
body
>
</
html
>
2.用DataView.RowFilter属性来筛选数据
DataView.RowFilter属性可以利用比较运算符 =、<、>、<=、>=以及Like来过滤记录中的数据。其实该属性需要的值就是一个Where子句
示例2:
<%
@ Page Language
=
"
C#
"
%>
<%
@ Import Namespace
=
"
System.Data
"
%>
<%
@ Import Namespace
=
"
System.Data.SqlClient
"
%>
<
script language
=
"
C#
"
runat
=
"
server
"
>
public
void
Page_Load(Object sender,EventArgs e) {
string
strConn
=
"
server=.;database=sango;uid=sa;pwd=
"
; SqlConnection conn
=
new
SqlConnection(strConn); DataSet ds
=
new
DataSet();
string
strCmd
=
"
SELECT * FROM people
"
; SqlDataAdapter sda
=
new
SqlDataAdapter(strCmd,conn); sda.Fill(ds,
"
tempTable
"
); DataTable dt
=
ds.Tables[
"
tempTable
"
];
//
循环得到原始数据
Response.Write(
"
<font color=red>原始数据:</font><br>
"
); for ( int i = 0 ;i < dt.Rows.Count;i ++ ) { Response.Write(dt.DefaultView[i][ " 姓名 " ] + " <br> " ); }
//
对数据进行筛选
dt.DefaultView.RowFilter
=
"
阵营='魏国' and 士兵>9000
"
; Response.Write(
"
<font color=red>筛选后的数据:</font><br>
"
); for ( int i = 0 ;i < dt.Rows.Count;i ++ ) { Response.Write(dt.DefaultView[i][ " 姓名 " ] + " <br> " ); }
}
</
script
>
3.用DataView.RowStateFilter属性得到操作后的数据
DataView.RowStateFilter所需要的值是DataViewState枚举值中的一个,DataViewRowState所包含的状态值如下表所示:
成员名称
说明
Added
新添加的行
CurrentRows
包括未更改行、新行和已修改行的当前行
Deleted
已删除的行
ModifiedCurrent
被修改后的记录
ModifiedOriginal
被修改前的记录
None
无
OriginalRows
包括未被修改以及删除掉的原始记录
Unchanged
未更改的行
示例3:
<%
@ Page Language
=
"
C#
"
%>
<%
@ Import Namespace
=
"
System.Data
"
%>
<%
@ Import Namespace
=
"
System.Data.SqlClient
"
%>
<
script
language
="c#"
runat
="server"
>
public
void
Page_Load(Object sender,EventArgs e) { string strConn
=
"
server=.;database=sango;uid=sa;pwd=
"
; string strCmd
=
"
SELECT * FROM people
"
; DataSet ds
=
new
DataSet(); SqlDataAdapter sda
=
new
SqlDataAdapter(strCmd,strConn); sda.Fill(ds,
"
tempTable
"
);
//
用Session在各函数之间传递原始表
Session[
"
myTable
"
]
=
ds.Tables[
"
tempTable
"
]; Response.Write(
"
<font color=red>新添加的记录是:</font><hr noshade>
"
);
this
.Show_Added(); Response.Write(
"
<p><font color=red>被删除的记录是:</font><hr noshade>
"
);
this
.Show_Deleted(); Response.Write(
"
<p><font color=red>修改后的记录是:</font><hr noshade>
"
);
this
.Show_ModifiedCurrent(); Response.Write(
"
<p><font color=red>修改前的记录是:</font><hr noshade>
"
);
this
.Show_ModifiedOriginal(); Response.Write(
"
<p><font color=red>尚未被更改的记录是:</font><hr noshade>
"
);
this
.Show_Unchanged(); Response.Write(
"
<p><font color=red>现在存在的记录是:</font><hr noshade>
"
);
this
.Show_CurrentRows(); Response.Write(
"
<p><font color=red>原始记录是:</font><hr noshade>
"
);
this
.Show_OriginalRows(); } private
void
Show_Added() { DataTable dt
=
(DataTable)Session[
"
myTable
"
];
//
DataTable有一个方法NewRow()来创建一个新记录对象,然后再分别赋值
DataRow newRecord1
=
dt.NewRow(); DataRow newRecord2
=
dt.NewRow(); newRecord1[
"
姓名
"
]
=
"
颜良
"
; newRecord1[
"
阵营
"
]
=
"
袁绍军
"
; newRecord2[
"
姓名
"
]
=
"
文丑
"
; newRecord2[
"
阵营
"
]
=
"
袁绍军
"
;
//
用DataTable.Rows.Add()方法添加数据
dt.Rows.Add(newRecord1); dt.Rows.Add(newRecord2);
//
设置状态为Added,并打印新添加的这两条数据
dt.DefaultView.RowStateFilter
=
DataViewRowState.Added;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { Response.Write(dt.DefaultView[i][
"
姓名
"
]
+
"
--
"
+
dt.DefaultView[i][
"
阵营
"
]
+
"
<br>
"
); } } private
void
Show_Deleted() { DataTable dt
=
(DataTable)Session[
"
myTable
"
];
//
从DataTable中删除数据,用DataTable.Select(string)方法创建一个DataRow数组,然后在循环中调用DataRow.Delete()来删除
string expression
=
"
阵营='蜀国' and 性别='女'
"
; DataRow[] tempRow
=
dt.Select(expression);
for
(
int
i
=
0
;i
<
tempRow.Length;i
++
) { tempRow[i].Delete(); }
//
状态设置为Deleted,并打印被删除的数据
dt.DefaultView.RowStateFilter
=
DataViewRowState.Deleted;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { Response.Write(dt.DefaultView[i][
"
姓名
"
]
+
"
--
"
+
dt.DefaultView[i][
"
阵营
"
]
+
"
<br>
"
); } } private
void
Show_ModifiedCurrent() { DataTable dt
=
(DataTable)Session[
"
myTable
"
];
//
得到需要修改的记录行
string expression
=
"
阵营='魏国' and 士兵<=10000
"
; DataRow[] tempRow
=
dt.Select(expression);
for
(
int
i
=
0
;i
<
tempRow.Length;i
++
) {
//
将每个带兵数小于9000的武将各加上1000
tempRow[i][
"
士兵
"
]
=
(
int
)tempRow[i][
"
士兵
"
]
+
1000
; }
//
状态设置为ModifiedCurrent,并打印修改后的记录
dt.DefaultView.RowStateFilter
=
DataViewRowState.ModifiedCurrent;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { Response.Write(dt.DefaultView[i][
"
姓名
"
]
+
"
--
"
+
dt.DefaultView[i][
"
士兵
"
]
+
"
<br>
"
); } } private
void
Show_ModifiedOriginal() {
//
这个函数得到相对于ModifiedCurrent修改前的记录
DataTable dt
=
(DataTable)Session[
"
myTable
"
];
//
状态设置为ModifiedOriginal,并打印修改前的记录
dt.DefaultView.RowStateFilter
=
DataViewRowState.ModifiedOriginal;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { Response.Write(dt.DefaultView[i][
"
姓名
"
]
+
"
--
"
+
dt.DefaultView[i][
"
士兵
"
]
+
"
<br>
"
); } } private
void
Show_Unchanged() { DataTable dt
=
(DataTable)Session[
"
myTable
"
];
//
得到所有列名,注意这里不能像DataRow那样直接创建数组,而是要使用DataColumnCollection集合来获取数组
DataColumnCollection cols
=
dt.Columns;
//
状态设置为Unchanged,并打印未修改的记录
dt.DefaultView.RowStateFilter
=
DataViewRowState.Unchanged;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { foreach(DataColumn col
in
cols) { Response.Write(dt.DefaultView[i][col.ColumnName]
+
"
"
); } Response.Write(
"
<p>
"
); } } private
void
Show_CurrentRows() { DataTable dt
=
(DataTable)Session[
"
myTable
"
]; DataColumnCollection cols
=
dt.Columns;
//
状态设置为CurrentRows,并打印当前存在的记录
dt.DefaultView.RowStateFilter
=
DataViewRowState.CurrentRows;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { foreach(DataColumn col
in
cols) { Response.Write(dt.DefaultView[i][col.ColumnName]
+
"
"
); } Response.Write(
"
<p>
"
); } } private
void
Show_OriginalRows() { DataTable dt
=
(DataTable)Session[
"
myTable
"
]; DataColumnCollection cols
=
dt.Columns;
//
状态设置为OriginalRows,并打印原始记录
dt.DefaultView.RowStateFilter
=
DataViewRowState.OriginalRows;
for
(
int
i
=
0
;i
<
dt.DefaultView.Count;i
++
) { foreach(DataColumn col
in
cols) { Response.Write(dt.DefaultView[i][col.ColumnName]
+
"
"
); } Response.Write(
"
<p>
"
); } }
</
script
>