Filter dataset in c# using Dataview.

Aman Sharma
We can filter data in Dataset in C# using DataRow. We can use multiple filters in Dataview rowfliter to filter data. We can filter any one table from dataset.

      DataView dv = new DataView(ds.Tables[0]);
      dv.RowFilter = "QuestionId<=5"; // You can add multiple conditions here in Row filter.

      DataSet dsTemp = new DataSet();// declare new dataset.

      DataTable dt = dv.ToTable();//declare new datatable and assign filtered data to table.
      dsTemp.Tables.Add(dt); // Add data table to dataset.

Before Filter:

After filter:

Full Example with Repeater:


Create Table in SQL Database:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
       <h4>Dynamic Control In Repeater</h4>
            <br />
            <br />
            <asp:Repeater ID="rptPrint" runat="server" >
                    <table style="width: 300px">
                            <td colspan="5" style="width: 7in">
                                    <asp:Label ID="LblQuestion" Text='<%#Eval("Question")%>' runat="server"></asp:Label></strong><br />
                                <asp:HiddenField ID="hfAnswer" Value='<%#Eval("Answer") %>' runat="server" />
                                <asp:HiddenField ID="hfType" Value='<%#Eval("Type") %>' runat="server" />
                                 <asp:HiddenField ID="hfQuestionId" Value='<%#Eval("QuestionId") %>' runat="server" />

                            <td colspan="4">
                                <asp:PlaceHolder ID="phRow" runat="server" />
                                <br />


Code Behind File:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    private void Fill_Repeater()
        if (con.State == ConnectionState.Open)

        SqlDataAdapter adp = new SqlDataAdapter("Select * from tblQuestion", con);
        DataSet ds = new DataSet();

        //Dataview to filter data in Dataset

        DataView dv = new DataView(ds.Tables[0]);
        dv.RowFilter = "QuestionId<=5"; // You can add multiple conditions here in Row Filter...

        DataSet dsTemp = new DataSet();
        DataTable dt = dv.ToTable();

        if (dsTemp.Tables[0].Rows.Count > 0)
            rptPrint.DataSource = dsTemp;

Post a Comment

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !