Graphics - Sekolah Tinggi Teknik Surabaya
Download
Report
Transcript Graphics - Sekolah Tinggi Teknik Surabaya
Lecture 8
DataGridView
Devi, Erick, Reddy
» DataSet
» DataTable
» DataGridView
» DataSet and Relational Database
» Simple Exercise
A memory-resident representation of
data that provides a consistent relational
programming model regardless of the
source of the data it contains
» DataTable
» DataRelation
˃ Relation between tables
» Constraints
DataSet
DataTable
…
DataTable
» Programmatically create
a DataTable, DataRelation,
and Constraint within a DataSet and
populate the tables with data.
» Populate the DataSet with tables of
data from an existing relational data
source using a DataAdapter.
» Load and persist the DataSet contents
using XML.
» Visually
» Programmatically
DataSet customerOrders =
new DataSet("CustomerOrders");
DataTable dt1 = new DataTable("Orders");
dt1.Columns.Add("OrderID", typeof(Int32));
dt1.Columns.Add("OrderQuantity", typeof(Int32));
dt1.Columns.Add("CompanyName", typeof(string));
dt1.PrimaryKey =
new DataColumn[] { dt1.Columns["OrderID"] };
DataSet customerOrders =
new DataSet("CustomerOrders");
customerOrders.Tables.Add(dt1);
customerOrders.Relations.Add(
"CustOrders",
customerOrders.Tables["Customers"].Columns["CustID"],
customerOrders.Tables["Orders"].Columns["CustID"]
);
DataTable workTable =
new DataTable("Customers");
DataTable workTable =
new DataTable("Customers");
DataColumn workCol =
workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;
workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));
DataColumn
DataRow
DataRow
DataRow
DataColumn
DataRow
DataRow
DataRow
DataRow workRow = workTable.NewRow();
workRow["CustLName"] = "Smith";
workRow[1] = "Smith";
workTable.Rows.Add(workRow);
foreach (DataRow row in workTable.Rows) {
foreach (DataColumn column in workTable.Columns)
Console.Write("\t{0}", row[column.ColumnName]);
}
» A visual component to display
DataTable
» Requires Data Source
» Editing Data
˃ Insert
˃ Update
˃ Delete
» Display data
»
»
»
»
»
»
»
AllowUserToAddRows
AllowUserToDeleteRows
ColumnHeadersVisible
DataSource
DefaultCellStyle
ReadOnly
SelectionMode
˃ Cell Select
˃ FullRowSelect
˃ FullColumnSelect
˃ RowHeaderSelect
˃ ColumnHeaderSelect
dataGridView1.DataSource =
workTable;
DataGridViewComboBoxCell x =
new DataGridViewComboBoxCell();
x.Items.Add("a");
x.Items.Add("b");
dataGridView1[0, 0] = x;
» Code below provides “a thousand
separator and 2 number after decimal”
format for the 3rd cell
dataGridView1.Columns[2].
DefaultCellStyle.Format = "N2";
» Refer to:
http://msdn.microsoft.com/enus/library/26etazsy(v=vs.110).aspx
A relational database is a database that
has a collection of tables of data items, all
of which is formally described and
organized according to the relational
model
» Has the same structure
» DataSet can be formed from relational
database
» It requires DataAdapter
DataSet
DataAdapter
Relational
Database
» It provides abstraction
˃ As DataSet created, it can be managed in
many ways
˃ Data Source may be changed flexibly
˃ It eases programmer working with many
different DBMSs; Only need to understand
DataSet, not SQL for every DBMS.
» Add student data to DataGridView
» DataSets, DataTables, and DataViews,
http://msdn.microsoft.com/enus/library/ss7fbaez(v=vs.110).aspx