Navigation

Friday, 7 December 2012

What is DataView in ASP.NET


  A DataView(DV) enables you to create different views of the data stored in a DataTable/DataSet, a capability that is often used in data-binding applications. Using a DV, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.
   A DV provides a dynamic view of data whose content, ordering, and membership reflect changes to the underlying DataTable/DataSet as they occur. This is different from the Select method of the DataTable/DataSet, which returns a DataRow array from a table per a particular filter and/or sort order and whose content reflects changes to the underlying table, but whose membership and ordering remain static. The dynamic capabilities of the DVmake it ideal for data-binding applications.
  A DV provides you with a dynamic view of a single set of data to which you can apply different sorting and filtering criteria, similar to the view provided by a database. However, a DVdiffers significantly from a database view in that the DVcannot be treated as a table and cannot provide a view of joined tables. You also cannot exclude columns that exist in the source table, nor can you append columns, such as computational columns, that do not exist in the source table.


How to Create DataView(DV)

SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=1234
;database=NewDB;");
                string strSQL = "Select * from Emp";
                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
                DataSet ds = new DataSet();
                dt.Fill(ds, "EMP");
                con.Close();
                **DataView dv = new DataView(ds.Tables[0]);               
                GridView1.DataSource = dv;**
                GridView1.DataBind();

** This is DataView(dv) Shows All Record of ds.table[0] table here u can filter on dataview 


Adding New Rows in DataView


                DataView dv = new DataView(ds.Tables[0]);
                DataRowView newrow = dv.AddNew();
                newrow["EmpID"] = 7;
                newrow["EName"] = "ram";
                newrow.EndEdit();
                GridView1.DataSource = dv;
                GridView1.DataBind();

Deleting in DataView

              DataView DVnew DataView(ds.Tables[0]);
      if (DV.Count > 0)
        {
            DV.Delete(0);
            DV[0].Row.AcceptChanges();
        }
                GridView1.DataSource = dv;
                GridView1.DataBind();




Filter in DataView

     DataView dv = new DataView(ds.Tables[0],"""EmpID>=753"DataViewRowState.CurrentRows);
                GridView1.DataSource = dv;
                GridView1.DataBind();

No comments:

Post a Comment