利用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>