0% found this document useful (0 votes)
14 views

Chapter 3 - Data Controls

Chapter 3 discusses advanced data controls in Windows Forms, focusing on DataGridView, ListView, and TreeView for managing and displaying data. It covers the creation, binding, and manipulation of data using DataTable and DataSet, as well as key properties and events associated with each control. The chapter provides practical code examples for implementing these controls in data-driven applications.

Uploaded by

Huy Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter 3 - Data Controls

Chapter 3 discusses advanced data controls in Windows Forms, focusing on DataGridView, ListView, and TreeView for managing and displaying data. It covers the creation, binding, and manipulation of data using DataTable and DataSet, as well as key properties and events associated with each control. The chapter provides practical code examples for implementing these controls in data-driven applications.

Uploaded by

Huy Phạm
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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 ADO.NET 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 ADO.NET 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


table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(int));

// Add the DataTable to the DataSet


dataSet.Tables.Add(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 = table.NewRow();
row["ID"] = 1;
row["Name"] = "Phương";
row["Age"] = 20;

// Add the row to the table


table.Rows.Add(row);

// Add another row directly


table.Rows.Add(2, “Nam", 21);

// Add the DataTable to the DataSet


dataSet.Tables.Add(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 = Convert.ToString(table.Rows[0][1]);

// Access a specific cell by column name


int age = Convert.ToInt32(table.Rows[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 }
};

dataGridView1.DataSource = 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 = System.IO.File.ReadAllLines(filePath);
if (dataLines.Length > 0)
{
string[] headers = dataLines[0].Split(',');
foreach (string header in headers)
{
dt.Columns.Add(header);
}

for (int i = 1; i < dataLines.Length; i++)


{
dt.Rows.Add(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)


{
dataGridView1.DataSource = ReadCsv("data.csv");
}
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
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Subject", typeof(string));
dt.Columns.Add("Score", typeof(double));

// Add data to table


dt.Rows.Add(1, "Nguyen Van A", "Math", 8.5);
dt.Rows.Add(2, "Tran Thi B", "Physics", 7.2);
dt.Rows.Add(3, "Le Van C", "Chemistry", 6.8);
dt.Rows.Add(4, "Pham Thi D", "English", 9.1);

// 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);

// Get data from specific row


DataGridViewRow row1 = dataGridView1.Rows[1];
string cellValue = Convert.ToString(row1.Cells[0].Value);

// Get data from selected row


DataGridViewRow selecRow = dataGridView1.SelectedRows[0];
string selectCell = Convert.ToString(selecRow.Cells[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:
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.GridLines = true;

// Add columns to ListView

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);
}

// Edit colums width


foreach (ColumnHeader column in listView1.Columns)
{
column.Width = -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 = listView1.Items[1].SubItems[1].Text; // Items: for Row , SubItems: for
Column (0-based index)

// Get data from selected row


ListViewItem item = listView1.SelectedItems[0]; // Get selected row
string cellValue = item.SubItems[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
parent1.Nodes.Add(child1);
parent1.Nodes.Add(child2);
parent2.Nodes.Add(child3);
parent2.Nodes.Add(child4);
parent3.Nodes.Add(child5);
parent3.Nodes.Add(child6);

// Add Parent Nodes to Root Nodes


root1.Nodes.Add(parent1);
root1.Nodes.Add(parent2);
root2.Nodes.Add(parent3);

// Add Root Nodes to TreeView


treeView1.Nodes.Add(root1);
treeView1.Nodes.Add(root2);

// Expand all nodes initially


treeView1.ExpandAll();
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 = treeView1.Nodes[0]; // get first node
string firstNodeText = firstNode.Text; // get data of node

// Get data from selected Node


string selectedNodeText = e.Node.Text;

// Get parent or child node


TreeNode firstChild = selectedNode.Nodes[0]; // Get first child node
TreeNode parent = selectedNode.Parent; // 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

You might also like