c# - How to filter data in a DataView

DataView RowFilter Property
.Net framework's DataView represents a data bindable, customized view of a DataTable for sorting, searching, filtering, navigation, and editing. DataView represents a connected view of its corresponding DataTable.

DataView does not stroe data. If we change the DataView data, it will affect the DataTable. If we change the DataTable's data, it will affect all associated DataViews. DataView class exists in System.Data namespace.

DataView class’s RowFilter property allows us to get or set the expression used to filter which rows are viewed in the DataView. RowFilter property value data type is String which represents a String that specifies how rows are to be filtered.

We can assign a RowFilter value as "FirstName = 'Innee'" where 'FirstName' is a column name followed by an operator (=) and a value 'Innee' to filter on. The value must be in quotation marks.

The following ado.net c# example code demonstrates to us how can we filter a DataView's rows to display programmatically in an asp.net application.
DataViewRowFilterProperty.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;
        dc1.Unique = true;

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "Category";
        dc2.DataType = typeof(string);

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "BookName";
        dc3.DataType = typeof(string);

        DataColumn dc4 = new DataColumn();
        dc4.ColumnName = "Author";
        dc4.DataType = typeof(string);

        dt.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3, dc4 });

        dt.Rows.Add(new object[] { 1, "Flash", "Flash Multiplayer Virtual Worlds", "Makzan" });
        dt.Rows.Add(new object[] { 2, ".NET", "Entity Framework Tutorial", "Joydip Kanjilal" });
        dt.Rows.Add(new object[] { 3, "Flash", "WordPress and Flash 10x Cookbook", "Peter Spannagle, Sarah Soward" });
        dt.AcceptChanges();

        GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();

        //this line create a new DataView
        DataView dView = new DataView(dt);
        dView.RowFilter = "Category = 'Flash'";

        Label1.Text = "Here we create a new DataView<br />" +
                      "and set the RowFilter expression (Category = 'Flash')";

        GridView2.DataSource = dView;
        GridView2.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DataView RowFilter Property in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            How to use DataView RowFilter Property in ado.net
        </h2>
        <hr width="575" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="Crimson" Height="35" />
            <RowStyle BackColor="MediumSeaGreen" />
            <AlternatingRowStyle BackColor="DarkGreen" />
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:GridView 
            ID="GridView2"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="575"
            Font-Names="Courier"
            >
            <HeaderStyle BackColor="Crimson" Height="35" />
            <RowStyle BackColor="MediumSeaGreen" />
            <AlternatingRowStyle BackColor="DarkGreen" />
        </asp:GridView>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>