CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to add new row in a DataView

The DataView provides different views of the data stored in a DataTable. DataViews can be created and configured on both design time and run time . We can create DataView in two ways. Either we can use the DataView constructor, or we can create a reference to the DefaultView Property of the DataTable. We can create multiple DataViews for any given DataTable. Note that if you create a DataView using the constructor that does not take any arguments, you will not be able to use the DataView until you have set the Table property.

We can add new rows in the DataView using AddNew method in the DataView. 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.

         C# Source Code Download           Print Source Code
         How to add new row in a DataView - Download
        
C# Tutorial

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());
            }
        }
    }
}



CSharp DataView Related Contents
*     How to create a DataView
*     How to Sort a DataView
*     How to Filter a DataView
*     How to Search in a DataView
*     How to update row in a DataView
*     How to delete row in a DataView
*     How to create a new DataTable from the DataView


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.