How to delete row in a DataView | C#

The DataView functionality offers versatile options for obtaining diverse views of the data stored in a DataTable. By utilizing a DataView, developers can utilize its capabilities to sort, filter, and search data within a DataTable. Moreover, DataView allows for the seamless addition of new rows and modification of existing content in the associated DataTable. This flexibility in data manipulation is particularly valuable in data-binding applications, where DataView enables the creation of custom views to meet specific requirements.

Delete row in a DataView

In addition to manipulating data, DataView also supports the deletion of data from the DataView. Through appropriate methods and techniques, developers can seamlessly remove data from the DataView, effectively managing the content displayed within the view.

The following C# source code shows how to delete the data from DataView.

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, "Delete Row"); adapter.Dispose(); command.Dispose(); connection.Close(); dv = new DataView(ds.Tables[0], "", "Product_ID", DataViewRowState.CurrentRows); dv.Table.Rows[3].Delete(); dataGridView1.DataSource = dv; } catch (Exception ex) { MessageBox.Show (ex.ToString()); } } } }

Conclusion

The DataView functionality offers valuable capabilities for creating diverse data views within a DataTable. With the ability to sort, filter, and search data, as well as add new rows and modify content, DataView empowers developers in data-binding applications to present and manipulate data according to specific requirements. The dynamic nature of DataView ensures real-time updates, reflecting changes made to the underlying DataTable. Additionally, DataView facilitates the deletion of data, allowing for comprehensive data management within the view.