下面就是利用RowFilter属性,将一个DataSet绑定到两个DataGrid的例子代码:
<%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" %>
<%@ 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>
<%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>" %>
<%@ 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>