How to Search in a DataView | C#

The DataView functionality offers various perspectives on the data stored within a DataTable. By utilizing the constructor of the DataView class, developers can initialize a new instance of the DataView class, with the DataTable serving as an argument. Creating a DataView can be accomplished through two distinct methods. One approach involves using the DataView Constructor directly, while the other entails creating a reference to the DefaultView property of the DataTable. Furthermore, it is possible to create multiple DataViews for a given DataTable, allowing for versatile data exploration from different angles.

A DataView provides a dynamic view of the data within the underlying DataTable, ensuring that changes made to the content, ordering, and membership are accurately reflected in real-time. This dynamic behavior enhances the flexibility and responsiveness of the DataView, allowing users to seamlessly adapt to evolving data requirements.

Search in a DataView

To facilitate efficient data retrieval within a DataView, the Find method can be employed. By utilizing the Find method, it becomes possible to search within a DataView based on the sort key values. The Find method returns an integer representing the index of the DataRowView that matches the specified search criteria. In the case where multiple rows satisfy the search criteria, only the index of the first matching DataRowView is returned. If no matches are found, the Find method returns -1, providing a reliable indicator of the search outcome.

int index = dv.Find("PRODUCT5");
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, "Find Row DataView"); adapter.Dispose(); command.Dispose(); connection.Close(); dv = new DataView(ds.Tables[0]); dv.Sort = "Product_Name"; int index = dv.Find("PRODUCT5"); if (index == -1) { MessageBox.Show ("Item Not Found"); } else { MessageBox.Show(dv[index]["Product_id"].ToString() + " " + dv[index]["Product_Name"].ToString()); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }

Conclusion

DataView operation empowers developers to access any point of data saved inside a DataTable. DataView constructor or default view property allows for the creation of multiple DataViews that are specialized for user exploration needs. DataViews reflect any changes made in DataTables in real time, which leads to a dynamic and responsive dashboard. Furthermore, the Find method enables users to search for specific DataView entries extremely quick, which escalates the whole retrieval process.