返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

利用RowFilter属性,将一个DataSet绑定到两个DataGrid源代码

时间:2011-05-31 20:10来源:知行网www.zhixing123.cn 编辑:麦田守望者

下面就是利用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>
 

------分隔线----------------------------
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
推荐内容
猜你感兴趣