How to Filter a DataView | C#

The DataView feature empowers you to generate diverse perspectives of the data residing within a DataTable. With DataView, you have the ability to customize and tailor the data views according to specific requirements. This powerful functionality enables sorting, filtering, and searching operations on a DataTable, in addition to facilitating the addition of new rows and modification of existing content.

Filter a DataView

DataView will consistently help data presentation for developers and they will be able to dynamically rice data presentation process to match the specific demands. Efficacy may be given to information sorting, displaying in the ascending order or in the descending order according to the fields. Filtering means in this instance that one can separate certain datasets according to their characteristics. This task is automated, leading to a simplification of the data collection process. Furthermore, exposed search commands can operate the DataTable to help you avoid stressful manual data research. Coming with not only input and edit application but also facilitate the ability to edit existing content in the DataTable which is one click away, DataView can do more than just opeations.

The following C# source code creates a view that shows all Product Details where the Product Price less than 3000. Create a new C# project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following C# Source Code on button click event.

Full Source C#
using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection connection ; SqlCommand command ; SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); DataView dv ; string sql = null; connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; sql = "Select * from product"; connection = new SqlConnection(connetionString); try { connection.Open(); command = new SqlCommand(sql, connection); adapter.SelectCommand = command; adapter.Fill(ds, "Filter DataView"); adapter.Dispose(); command.Dispose(); connection.Close(); dv = new DataView(ds.Tables[0], "Product_Price < = 3000", "Product_Name", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv; } catch (Exception ex) { MessageBox.Show (ex.ToString()); } } } }

Conclusion

Dataview is a powerful tool that is great for creating customized views of data that exists in a DataTable. Its powerful processing performance allows this technology to be used for sorting, filtering, searching, and even data manipulation so developers can present data for users with specific needs. Many different DataViews can be set up with one DataTable, and that allows various angles of looking at the data at the same time. DataView functionality enables a developer to put all pieces together and to help their data facilitate effective data analysis and decision-making processes.