How to add new row in a DataView | C#

The DataView functionality offers versatile options for obtaining distinct views of the data stored in a DataTable. DataViews can be created and configured at both design time and runtime, providing flexibility in adapting to various scenarios. There are two primary approaches to creating a DataView: utilizing the DataView constructor or establishing a reference to the DefaultView property of the DataTable. Moreover, it is possible to create multiple DataViews for a single DataTable, allowing for diverse perspectives on the data.

It is important to note that if a DataView is created using the constructor without any arguments, the DataView cannot be utilized until the Table property is set. This requirement ensures that the DataView is properly associated with the underlying DataTable before it can be effectively employed.

Add new rows to a DataView

To add new rows to a DataView, the AddNew method can be utilized. By invoking the AddNew method within the DataView, developers can seamlessly insert new rows into the DataView, enabling the dynamic expansion and modification of the underlying data representation.

The following C# source code shows how to add new row in a DataView . 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, "Add New"); adapter.Dispose(); command.Dispose(); connection.Close(); dv = new DataView(ds.Tables[0]); DataRowView newRow = dv.AddNew(); newRow["Product_ID"] = 7; newRow["Product_Name"] = "Product 7"; newRow["Product_Price"] = 111; newRow.EndEdit(); dv.Sort = "product_id"; dataGridView1.DataSource = dv; } catch (Exception ex) { MessageBox.Show (ex.ToString()); } } } }

Conclusion

The DataView functionality offers valuable capabilities for generating different views of data stored within a DataTable. The ability to create and configure DataViews at both design time and runtime provides flexibility and adaptability. Multiple DataViews can be created for a single DataTable, facilitating comprehensive data exploration. It is important to ensure that the DataView is associated with the underlying DataTable before utilizing it. Additionally, the AddNew method empowers developers to seamlessly add new rows to the DataView, enhancing the dynamic nature of the data representation.