ekko un esempio:
codice:
private void DemonstrateDataView(){
// Create one DataTable with one column.
DataTable myTable = new DataTable("myTable");
DataColumn colItem = new DataColumn("item",Type.GetType("System.String"));
myTable.Columns.Add(colItem);
// Add five items.
DataRow NewRow;
for(int i = 0; i <5; i++){
NewRow = myTable.NewRow();
NewRow["item"] = "Item " + i;
myTable.Rows.Add(NewRow);
}
// Change the values in the table.
myTable.Rows[0]["item"]="cat";
myTable.Rows[1]["item"] = "dog";
myTable.AcceptChanges();
// Create two DataView objects with the same table.
DataView firstView = new DataView(myTable);
DataView secondView = new DataView(myTable);
// Print current table values.
PrintTableOrView(myTable,"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 myDataRowView;
myDataRowView=secondView.AddNew();
myDataRowView["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 t, string label){
// This function prints values in the table or DataView.
Console.WriteLine("\n" + label);
for(int i = 0; i<t.Rows.Count;i++){
Console.WriteLine("\t" + t.Rows[i]["item"]);
}
Console.WriteLine();
}
private void PrintTableOrView(DataView dv, string label){
// This overload prints values in the table or DataView.
Console.WriteLine("\n" + label);
for(int i = 0; i<dv.Count;i++){
Console.WriteLine("\t" + dv[i]["item"]);
}
Console.WriteLine();
}
codice:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
public class DataGridSample:Form{
DataSet ds;
DataGrid myGrid;
static void Main(){
Application.Run(new DataGridSample());
}
public DataGridSample(){
InitializeComponent();
}
void InitializeComponent(){
this.ClientSize = new System.Drawing.Size(550, 450);
myGrid = new DataGrid();
myGrid.Location = new Point (10,10);
myGrid.Size = new Size(500, 400);
myGrid.CaptionText = "Microsoft .NET DataGrid";
this.Text = "C# Grid Example";
this.Controls.Add(myGrid);
ConnectToData();
myGrid.SetDataBinding(ds, "Suppliers");
}
void ConnectToData(){
// Create the ConnectionString and crete a SqlConnection.
// Change the data source value to the name of your computer.
string cString = "user id=sa;" +
"password=;" +
"initial catalog=northwind;" +
"data source=MyComputerName\\NetSDK;" +
"Connect Timeout=5";
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "Suppliers");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
myConnection);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("Customers");
myAdapter.Fill(ds);
// Create a second Adapter and Command.
SqlDataAdapter adpProducts = new SqlDataAdapter();
adpProducts.TableMappings.Add("Table", "Products");
SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products",
myConnection);
adpProducts.SelectCommand = cmdProducts;
adpProducts.Fill(ds);
myConnection.Close();
Console.WriteLine("The connection is closed.");
System.Data.DataRelation dr;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
// Get the parent and child columns of the two tables.
dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
dc2 = ds.Tables["Products"].Columns["SupplierID"];
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
ds.Relations.Add(dr);
}
}