Chapter 3 - Data Controls
Chapter 3 - Data Controls
WINDOWS FORMS
APPLIED PROGRAMMING IN ENGINEERING
PROGRAMMING
// Create a DataTable
DataTable table = new DataTable("Students");
• 3.1.2 DataGridView:
⚬ DataGridView:
⚬ The DataGridView control in Windows Forms is a powerful tool for
displaying, editing, and managing tabular data. It displays data in a grid
format (rows & columns).
⚬ It can be used to show data from databases, collections, or manually added
rows.
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ DataGridView:
⚬ Key Properties of DataGridView:
⚬ DataSource: Binds the DataGridView to a data source.
⚬ Columns: Defines the structure of the table.
⚬ Rows: Contains the data in the grid.
⚬ AllowUserToAddRows: Enables or disables adding new rows.
⚬ ReadOnly: Prevents data modification.
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ DataGridView:
⚬ Data binding:
⚬ Data binding is the process of connecting a user interface (UI)
element to a data source, allowing data to be automatically
retrieved, displayed, updated, and synchronized between the UI and
the underlying data model.
⚬ DataGridView supports data binding from List, File (csv file) or
database.
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ Data binding from List:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
• 3.1.2 DataGridView:
⚬ DataGridView:
⚬ Data binding from CSV file:
⚬ Read data from CSV file and convert to DataTable.
⚬ Bind data to DataGridView.
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ Data binding from CSV file:
private DataTable ReadCsv(string filePath)
{
DataTable dt = new DataTable();
string[] dataLines = System.IO.File.ReadAllLines(filePath);
if (dataLines.Length > 0)
{
string[] headers = dataLines[0].Split(',');
foreach (string header in headers)
{
dt.Columns.Add(header);
}
• 3.1.2 DataGridView:
⚬ Data binding from CSV file:
• 3.1.2 DataGridView:
⚬ Data binding from DataTable:
// Create DataTable
DataTable dt = new DataTable("BangDiem");
// Add columns
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Subject", typeof(string));
dt.Columns.Add("Score", typeof(double));
// Binding data
dataGridView1.DataSource = dt;
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ Get data from DataGridView:
// Get data from Cell
string value = Convert.ToString(dataGridView1.Rows[0].Cells[0].Value);
• 3.1.2 DataGridView:
⚬ DataGridView:
⚬ Key Events of DataGridView:
⚬ CellClick: triggers when a user clicks on a cell.
⚬ CellContentClick: triggers when a user clicks on the actual
content (button, link, checkbox) in the cell.
3 . 1 D ATA C O N T R O L S :
• 3.1.3 ListView:
⚬ ListView:
⚬ The ListView is a multi-column list control that displays data in different
view modes.
⚬ It supports multiple columns, icons, checkboxes, and details view.
3 . 1 D ATA C O N T R O L S :
• 3.1.3 ListView:
⚬ ListView:
⚬ Key Properties of ListView:
⚬ View: Sets the display mode (Details, LargeIcon, SmallIcon, List,
Tile).
⚬ Columns: Holds column headers (used in Details view).
⚬ Items: Collection of items displayed in ListView.
⚬ CheckBoxs: Enables checkboxes for item selection.
⚬ FullRowSelect: Highlights the entire row when selected.
3 . 1 D ATA C O N T R O L S :
• 3.1.3 ListView:
⚬ Data binding from DataTable:
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.GridLines = true;
listView1.Columns.Clear();
foreach (DataColumn col in dt.Columns)
{
listView1.Columns.Add(col.ColumnName, 100);
}
3 . 1 D ATA C O N T R O L S :
• 3.1.3 ListView:
⚬ Data binding from DataTable:
// Add data to ListView
listView1.Items.Clear();
foreach (DataRow row in dt.Rows)
{
ListViewItem item = new ListViewItem(row[0].ToString()); // Cột đầ
u tiên
for (int i = 1; i < dt.Columns.Count; i++)
{
item.SubItems.Add(row[i].ToString());
}
listView1.Items.Add(item);
}
• 3.1.3 ListView:
⚬ Get data from ListView:
// Get data from Cell
string value = listView1.Items[1].SubItems[1].Text; // Items: for Row , SubItems: for
Column (0-based index)
• 3.1.3 ListView:
⚬ ListView:
⚬ Key Events of ListView:
⚬ SelectedIndexChanged: triggers when the selection changes in a
ListView.
⚬ ItemSelectionChanged: triggers when each item that gets
selected/deselected (useful in multi-selection scenarios).
3 . 1 D ATA C O N T R O L S :
• 3.1.4 TreeView:
⚬ TreeView:
⚬ TreeView in C# is a control used to display hierarchical data in a tree
structure.
⚬ Each item in a TreeView is called a TreeNode, which can contain multiple
child TreeNode elements.
3 . 1 D ATA C O N T R O L S :
• 3.1.4 TreeView:
⚬ TreeNode: It represents a single node (item) in the tree hierarchy and can
have child nodes.
⚬ RootNode: A RootNode in a TreeView is the top-most node in the hierarchy.
It serves as the base of the tree structure and can have multiple child
nodes.
⚬ ParentNode: A ParentNode in a TreeView is a node that has one or more
child nodes. It represents a higher level in the tree hierarchy.
⚬ ChildNode: A ChildNode in a TreeView is any node that is added under
another node (parent node). It helps create hierarchical structures.
3 . 1 D ATA C O N T R O L S :
• 3.1.4 TreeView:
⚬ Create Node:
TreeNode root1 = new TreeNode("Root 1");
TreeNode root2 = new TreeNode("Root 2");
• 3.1.4 TreeView:
⚬ Link Node:
// Add Children to Parent Nodes
parent1.Nodes.Add(child1);
parent1.Nodes.Add(child2);
parent2.Nodes.Add(child3);
parent2.Nodes.Add(child4);
parent3.Nodes.Add(child5);
parent3.Nodes.Add(child6);
• 3.1.4 TreeView:
⚬ Get data from TreeView:
⚬ In TreeView, you can access selected nodes, parent-child
relationships, or iterate through all nodes.
// Get data from specific Node
TreeNode firstNode = treeView1.Nodes[0]; // get first node
string firstNodeText = firstNode.Text; // get data of node
• 3.1.4 TreeView:
⚬ TreView:
⚬ Key Events of TreeView:
⚬ AfterSelect: triggers when the user selects a node, either by clicking
or using the keyboard.
⚬ NodeMouseClick: event is triggered when a user clicks on a node,
allows to handle left or right-clicks on nodes.
APPLIED PROGRAMMING IN
ENGINEERING
T H A N K S YO U