Sunday, August 26, 2007

ADO.net > DataView > Code Listing

DATAVIEW CODE LISTING

DataTable customers = new DataTable("Customers");
string cmdStr = ConfigurationManager.ConnectionStrings["nwind"].ConnectionString;

using (SqlConnection connection = new SqlConnection(cmdStr))
{
SqlCommand selectAllCustomers = connection.CreateCommand();
selectAllCustomers.CommandText = "Select * from [Customers]";

connection.Open();
customers.Load(selectAllCustomers.ExecuteReader(CommandBehavior.CloseConnection));
}

DataView dv = new DataView(customers, "Region= 'SP' and Country = 'Brazil'", "ContactName", DataViewRowState.CurrentRows);

GridView1.DataSource = dv;
GridView1.DataBind();

No comments: