如何利用RowFilter 属性从一个dataset中创建两个不同的datagrid

发表于:2007-06-30来源:作者:点击数: 标签:
Women Name Age Gender Staci 22 F Niki 21 F Candy 44 F Erica 32 F Andrea 23 F Susan 36 F Men Name Age Gender Alex 24 M John 39 M Chad 23 M Greg 48 M %@ Page Language= VB % %@ Import Namespace=System.Data % %@ Import Namespace=System.Data.Sql
Women
NameAgeGender
Staci 22 F
Niki 21 F
Candy 44 F
Erica 32 F
Andrea 23 F
Susan 36 F

Men
NameAgeGender
Alex 24 M
John 39 M
Chad 23 M
Greg 48 M


<%@ 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(ConfigurationSettings.AppSettings("DSN_aspa"))
        Dim myAdapter As New SqlDataAdapter("SELECT PersonName,PersonAge,PersonGender FROM tblPeople", myConnection)
    
        Dim myDataSet As New DataSet()
    
        Try
            myAdapter.Fill(myDataSet, "People")
    
            Dim myDataView As DataView = myDataSet.Tables("People").DefaultView
        
            myDataView.RowFilter = "PersonGender=@#F@#"
            myFemaleDataGrid.DataSource = myDataView
            myFemaleDataGrid.DataBind()
    
            myDataView.RowFilter = "PersonGender=@#M@#"
            myMaleDataGrid.DataSource = myDataView
            myMaleDataGrid.DataBind()
    Catch myException As Exception
        Response.Write("An error has oclearcase/" target="_blank" >ccurred: " & myException.ToString())
    End Try
    
    End Sub
</script>
<html>
    <head>
    </head>
    <body>
        <form runat="server">
            <p>
            Women<br>
            <asp:datagrid id="myFemaleDataGrid" runat="server" autogeneratecolumns="false" enableviewstate="false">
                <columns>
                        <asp:templatecolumn headertext="Name">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonName") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Age">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonAge") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Gender">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonGender") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                </columns>
            </asp:datagrid>
            </p>
            <p>
            Men<br>
            <asp:datagrid id="myMaleDataGrid" runat="server" autogeneratecolumns="false" enableviewstate="false">
                <columns>
                        <asp:templatecolumn headertext="Name">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonName") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Age">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonAge") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Gender">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonGender") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                </columns>
            </asp:datagrid>
            </p>
        </form>
    </body>
</html>


原文转自:http://www.ltesting.net