This is a discussion on c#.net within the C# Programming forums, part of the Software Development category; what is dataview and how it is better than datagrid in performance ?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| The DataView class can be used to create a new view of the table that can be filtered or sorted without affecting the underlying table. The DataView is similar to a table in that it has a collection of rows but the objects in the DataView’s Rows collection are of type DataRowView. You use the DataRowView the same as you would use a DataRow. You use the name of the field as an index to get the value from the column. Once you have a DataView, you can filter or sort the data without affecting the actual table. |
| |||
| Sample of using DataView Code: DataView custDV = new DataView(custDS.Tables["Customers"],
"Country = 'USA'",
"ContactName",
DataViewRowState.CurrentRows); Code: DataView custDV = custDS.Tables["Customers"].DefaultView; |
| |||
| A simple example for DataView Code: private void DemonstrateDataView()
{
// Create one DataTable with one column.
DataTable table = new DataTable("table");
DataColumn colItem = new DataColumn("item",
Type.GetType("System.String"));
table.Columns.Add(colItem);
// Add five items.
DataRow NewRow;
for(int i = 0; i <5; i++)
{
NewRow = table.NewRow();
NewRow["item"] = "Item " + i;
table.Rows.Add(NewRow);
}
// Change the values in the table.
table.Rows[0]["item"]="cat";
table.Rows[1]["item"] = "dog";
table.AcceptChanges();
// Create two DataView objects with the same table.
DataView firstView = new DataView(table);
DataView secondView = new DataView(table);
// Print current table values.
PrintTableOrView(table,"Current Values in Table");
// Set first DataView to show only modified
// versions of original rows.
firstView.RowStateFilter=DataViewRowState.ModifiedOriginal;
// Print values.
PrintTableOrView(firstView,"First DataView: ModifiedOriginal");
// Add one New row to the second view.
DataRowView rowView;
rowView=secondView.AddNew();
rowView["item"] = "fish";
// Set second DataView to show modified versions of
// current rows, or New rows.
secondView.RowStateFilter=DataViewRowState.ModifiedCurrent
| DataViewRowState.Added;
// Print modified and Added rows.
PrintTableOrView(secondView,
"Second DataView: ModifiedCurrent | Added");
}
private void PrintTableOrView(DataTable table, string label)
{
// This function prints values in the table or DataView.
Console.WriteLine("\n" + label);
for(int i = 0; i<table.Rows.Count;i++)
{
Console.WriteLine("\table" + table.Rows[i]["item"]);
}
Console.WriteLine();
}
private void PrintTableOrView(DataView view, string label)
{
// This overload prints values in the table or DataView.
Console.WriteLine("\n" + label);
for(int i = 0; i<view.Count;i++)
{
Console.WriteLine("\table" + view[i]["item"]);
}
Console.WriteLine();
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hi there, here is another example for your reference... ![]() Code: // create a view that can be filtered and sorted
DataView view = new DataView(ds.Tables[0]);
foreach (DataRowView drv in view) {
Console.WriteLine(drv["Name"].ToString());
}
// find the record for Rick
view.RowFilter = "Name = 'Evy'";
foreach (DataRowView drv in view) {
Console.WriteLine(drv["Name"].ToString());
}
__________________ cheers Aman |
| |||
| hi, The DataGrid control displays tabular data and optionally supports selecting, sorting, paging, and editing the data. By default, DataGrid generates a BoundColumn for each field in the data source (AutoGenerateColumns=true). Each field in the data is rendered in a separate column, in the order it occurs in the data. Field names appear in the grid's column headers, and values are rendered in text labels. A default format is applied to non-string values.
__________________ Venkat knowledge is Power |
| |||
| hi, here is the example <%@ Import Namespace="System.Data" %> <html> <script language="C#" runat="server"> ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime))); dt.Columns.Add(new DataColumn("BoolValue", typeof(bool))); dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Item " + i.ToString(); dr[2] = DateTime.Now; dr[3] = (i % 2 != 0) ? true : false; dr[4] = 1.23 * (i+1); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } void Page_Load(Object sender, EventArgs e) { MyDataGrid.DataSource = CreateDataSource(); MyDataGrid.DataBind(); } </script> <body> <h3><font face="Verdana">Simple DataGrid Example</font></h3> <form runat=server> <ASP ataGrid id="MyDataGrid" runat="server"BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" /> </form> </body> </html>
__________________ Venkat knowledge is Power |
![]() |
| Thread Tools | |
| Display Modes | |
| |