Home » Blog » 19.ADO.NET – RowFilter

19.ADO.NET – RowFilter

http://godffs.tistory.com/entry/19ADONET-RowFilter

 

DataView에 표시할 행을 필터링하는 데 사용하는 식을 사져오거나 설정
데이터 검색시 사용하는 속성 : “CategoryName Like ‘%컴퓨터%'”
Connection->Command->DataAdapter->DataSet->DataTable->Dataview->Dataview.RowFilger 속성

FrmRowFilter.aspx
<html xmlns=”http://www.w3.org/1999/xhtml”><head runat=”server”>

    <title></title>

</head>

<body>

    <form id=”form1″ runat=”server”>

    <div>

   

        카테고리명 :

        <asp:TextBox ID=”txtSearchQuery” runat=”server”></asp:TextBox>

        <asp:Button ID=”btnSearch” runat=”server” Text=”검색”onclick=”btnSearch_Click” />

        <asp:GridView ID=”ctlSearchList” runat=”server”>

        </asp:GridView>

        <br />

   

    </div>

    </form>

</body>

</html>

FrmRowFilter.aspx.cs
protected void btnSearch_Click(object sender, EventArgs e){

   SqlConnection con = new SqlConnection(

      ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString);

   con.Open();

   SqlCommand cmd = new SqlCommand(“Select * From Categories”, con);

   cmd.CommandType = CommandType.Text;

   SqlDataAdapter da = new SqlDataAdapter(cmd);

   DataSet ds = new DataSet();

   da.Fill(ds, “Categories”);

   //[1] DataTable로 변환

   DataTable dt = ds.Tables[0];

   //[2] DataView로 변환

   DataView dv = dt.DefaultView;

   //[3] DataView.RowFilter로 필터링

   dv.RowFilter = “CategoryName Like ‘%” + txtSearchQuery.Text + “%'”; // 필터링

   dv.Sort = “CategoryName Asc”; // 정렬

   this.ctlSearchList.DataSource = dv; // 필터링된 리스트만 출력됨.

   this.ctlSearchList.DataBind();

  

   con.Close();

}

결과화면