chenjinjie的专栏

=====初学者的专栏=====

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

 

阅读更多
个人分类: DataGrid专集
上一篇在DataGrid快速添加新行
下一篇为DataGrid添加CheckBox控件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭