CHAPTER 3: ADVANCED
WINDOWS FORMS
APPLIED PROGRAMMING IN ENGINEERING
PROGRAMMING
M.E. LE THANH TUNG
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ Data controls in Windows Forms are used to manage, display, and
manipulate data from different sources such as databases, collections, or
files.
⚬ These controls help in building data-driven applications efficiently.
⚬ Some Data controls in Windows Form:
⚬ DataGridView: Displays data in a grid format (rows and columns).
⚬ ListView: Displays data in different views (List, Details, LargeIcon,
SmallIcon, Tile).
⚬ TreeView: Displays hierarchical data (parent-child relationship).
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ DataTable & DataSet:
⚬ A DataTable in C# is a structure used to store tabular data in memory. It
is part of [Link] and is useful for working with datasets without requiring
a direct connection to a database.
⚬ A DataSet is a collection of DataTable objects used to store and manage
data in memory. It is commonly used in applications that interact with
databases.
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ DataTable & DataSet:
⚬ A DataTable in C# is a structure used to store tabular data in memory. It
is part of [Link] and is useful for working with datasets without requiring
a direct connection to a database.
⚬ A DataSet is a collection of DataTable objects used to store and manage
data in memory. It is commonly used in applications that interact with
databases.
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ Creating DataSet & DataTable:
// Create a DataSet
DataSet dataSet = new DataSet("MyDataSet");
// Create a DataTable
DataTable table = new DataTable("Students");
// Add columns to the DataTable
[Link]("ID", typeof(int));
[Link]("Name", typeof(string));
[Link]("Age", typeof(int));
// Add the DataTable to the DataSet
[Link](table);
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ Adding & Accessing data:
// Create a new row
DataRow row = [Link]();
row["ID"] = 1;
row["Name"] = "Phương";
row["Age"] = 20;
// Add the row to the table
[Link](row);
// Add another row directly
[Link](2, “Nam", 21);
// Add the DataTable to the DataSet
[Link](table);
3 . 1 D ATA C O N T R O L S :
• 3.1.1 Data controls:
⚬ Adding & Accessing data:
// Access a specific cell by column index
string name = [Link]([Link][0][1]);
// Access a specific cell by column name
int age = Convert.ToInt32([Link][0]["Age"]);
3 . 1 D ATA C O N T R O L S :
• 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; }
}
private void Form1_Load(object sender, EventArgs e)
{
List<Product> productList = new List<Product>
{
new Product { ID = 1, Name = "Laptop", Price = 1200 },
new Product { ID = 2, Name = "Mouse", Price = 20 },
new Product { ID = 3, Name = "Keyboard", Price = 45 }
};
[Link] = productList; // Bind list to DataGridView
}
3 . 1 D ATA C O N T R O L S :
• 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 = [Link](filePath);
if ([Link] > 0)
{
string[] headers = dataLines[0].Split(',');
foreach (string header in headers)
{
[Link](header);
}
for (int i = 1; i < [Link]; i++)
{
[Link](dataLines[i].Split(','));
}
}
return dt;
}
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ Data binding from CSV file:
private void Form1_Load(object sender, EventArgs e)
{
[Link] = ReadCsv("[Link]");
}
3 . 1 D ATA C O N T R O L S :
• 3.1.2 DataGridView:
⚬ Data binding from DataTable:
// Create DataTable
DataTable dt = new DataTable("BangDiem");
// Add columns
[Link]("StudentID", typeof(int));
[Link]("Name", typeof(string));
[Link]("Subject", typeof(string));
[Link]("Score", typeof(double));
// Add data to table
[Link](1, "Nguyen Van A", "Math", 8.5);
[Link](2, "Tran Thi B", "Physics", 7.2);
[Link](3, "Le Van C", "Chemistry", 6.8);
[Link](4, "Pham Thi D", "English", 9.1);
// Binding data
[Link] = 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 = [Link]([Link][0].Cells[0].Value);
// Get data from specific row
DataGridViewRow row1 = [Link][1];
string cellValue = [Link]([Link][0].Value);
// Get data from selected row
DataGridViewRow selecRow = [Link][0];
string selectCell = [Link]([Link][0].Value);
3 . 1 D ATA C O N T R O L S :
• 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:
[Link] = [Link];
[Link] = true;
[Link] = true;
// Add columns to ListView
[Link]();
foreach (DataColumn col in [Link])
{
[Link]([Link], 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
[Link]();
foreach (DataRow row in [Link])
{
ListViewItem item = new ListViewItem(row[0].ToString()); // Cột đầ
u tiên
for (int i = 1; i < [Link]; i++)
{
[Link](row[i].ToString());
}
[Link](item);
}
// Edit colums width
foreach (ColumnHeader column in [Link])
{
[Link] = -2;
}
3 . 1 D ATA C O N T R O L S :
• 3.1.3 ListView:
⚬ Get data from ListView:
// Get data from Cell
string value = [Link][1].SubItems[1].Text; // Items: for Row , SubItems: for
Column (0-based index)
// Get data from selected row
ListViewItem item = [Link][0]; // Get selected row
string cellValue = [Link][1].Text; // Get value from second column
3 . 1 D ATA C O N T R O L S :
• 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");
// Create Parent Nodes
TreeNode parent1 = new TreeNode("Parent 1");
TreeNode parent2 = new TreeNode("Parent 2");
TreeNode parent3 = new TreeNode("Parent 3");
// Create Child Nodes
TreeNode child1 = new TreeNode("Child 1");
TreeNode child2 = new TreeNode("Child 2");
TreeNode child3 = new TreeNode("Child 3");
TreeNode child4 = new TreeNode("Child 4");
TreeNode child5 = new TreeNode("Child 5");
TreeNode child6 = new TreeNode("Child 6");
3 . 1 D ATA C O N T R O L S :
• 3.1.4 TreeView:
⚬ Link Node:
// Add Children to Parent Nodes
[Link](child1);
[Link](child2);
[Link](child3);
[Link](child4);
[Link](child5);
[Link](child6);
// Add Parent Nodes to Root Nodes
[Link](parent1);
[Link](parent2);
[Link](parent3);
// Add Root Nodes to TreeView
[Link](root1);
[Link](root2);
// Expand all nodes initially
[Link]();
3 . 1 D ATA C O N T R O L S :
• 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 = [Link][0]; // get first node
string firstNodeText = [Link]; // get data of node
// Get data from selected Node
string selectedNodeText = [Link];
// Get parent or child node
TreeNode firstChild = [Link][0]; // Get first child node
TreeNode parent = [Link]; // Get parent node
3 . 1 D ATA C O N T R O L S :
• 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