利用RowFilter属性,将一个DataSet绑定到两个DataGrid
2024-07-21 02:23:07
供稿:网友
下面就是利用rowfilter属性,将一个dataset绑定到两个datagrid的例子代码:
<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script runat="server">
sub page_load(sender as object, e as eventargs)
dim myconnection as new sqlconnection("server=.;uid=sa;pwd=;database=pubs")
dim myadapter as new sqldataadapter("select au_fname,phone,contract from authors", myconnection)
dim mydataset as new dataset()
try
myadapter.fill(mydataset, "test1")
dim mydataview as dataview = mydataset.tables("test1").defaultview
mydataview.rowfilter = "contract=false"
myfemaledatagrid.datasource = mydataview
myfemaledatagrid.databind()
mydataview.rowfilter = "contract=true"
mymaledatagrid.datasource = mydataview
mymaledatagrid.databind()
catch myexception as exception
response.write("错误: " & myexception.tostring())
end try
end sub
</script>
<html>
<head>
</head>
<body>
<form runat="server" id="form1">
<p>
contract[false]:<br>
<asp:datagrid id="myfemaledatagrid" runat="server" autogeneratecolumns="false" enableviewstate="false">
<columns>
<asp:templatecolumn headertext="name">
<itemtemplate>
<span><%# container.dataitem("au_fname") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="contract">
<itemtemplate>
<span><%# container.dataitem("contract") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="phone">
<itemtemplate>
<span><%# container.dataitem("phone") %></span>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</p>
<p>
contract[true]:<br>
<asp:datagrid id="mymaledatagrid" runat="server" autogeneratecolumns="false" enableviewstate="false">
<columns>
<asp:templatecolumn headertext="name">
<itemtemplate>
<span><%# container.dataitem("au_fname") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="contract">
<itemtemplate>
<span><%# container.dataitem("contract") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="phone">
<itemtemplate>
<span><%# container.dataitem("phone") %></span>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</p>
</form>
</body>
</html>