-1-
-2-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
// Put the next line into the Declarations section.
private System.Data.DataSet dataSet;
public Form1()
{
InitializeComponent();
// Run all of the functions.
MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
dataSet = new DataSet();
// Add the new DataTable to the DataSet.
dataSet.Tables.Add(table);
// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i <= 2; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
}
private void MakeChildTable()
{
// Create a new DataTable.
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ChildID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ChildItem";
column.AutoIncrement = false;
column.Caption = "ChildItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
column.AutoIncrement = false;
column.Caption = "ParentID";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataSet.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["childID"] = i;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 0;
table.Rows.Add(row);
}
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["childID"] = i + 5;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 1;
table.Rows.Add(row);
}
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["childID"] = i + 10;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 2;
table.Rows.Add(row);
}
}
private void MakeDataRelation()
{
// DataRelation requires two DataColumn
// (parent and child) and a name.
DataColumn parentColumn =
dataSet.Tables["ParentTable"].Columns["id"];
DataColumn childColumn =
dataSet.Tables["ChildTable"].Columns["ParentID"];
DataRelation relation = new
DataRelation("parent2Child", parentColumn, childColumn);
dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
}
private void BindToDataGrid()
{
// Instruct the DataGrid to bind to the DataSet, with the
// ParentTable as the topmost DataTable.
BindingSource bs2 = new BindingSource();
bs2.DataSource = dataSet.Tables["ParentTable"];
// Bind data to DataGridView.DataSource
dataGrid1.DataSource = bs2;
BindingSource bs3 = new BindingSource();
bs3.DataSource = dataSet.Tables["ChildTable"];
// Bind data to DataGridView.DataSource
dataGrid2.DataSource = bs3;
}
private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
اسم الملف | لينك |
---|---|
تعلم لغة c sharp من البداية الى الاحتراف مهندس بديع موسى راجى | https://www.youtube.com/playlist?list=PLMmy9Ec9B98xzr_x2vafMGx9_gCIkZhM6 |
كورس Object-oriented programming C Sharp مهندس بديع موسى راجى | https://www.youtube.com/playlist?list=PLMmy9Ec9B98yRqBvoBB2Wi3mNro5DDdOL |
Database Analysis and design Full course - ERD schema مهندس بديع موسى راجى | https://www.youtube.com/playlist?list=PLMmy9Ec9B98zl-h_NvnQAs8BW8QRMIw-P |
تعلم باحتراف MS SQL server 2019 مهندس بديع موسى راجى | https://www.youtube.com/playlist?list=PLMmy9Ec9B98zuTzQXSZYomkE-RLntASLB |
كورس Windows Desktop Applications in c sharp مهندس بديع موسى راجى | https://www.youtube.com/playlist?list=PLMmy9Ec9B98yVy2t1hE3t4t1zKAxAvpkW |
مهنس بديع موسى راجى dbase in c sharp سى شارب و قواعد البيانات | https://www.youtube.com/playlist?list=PLMmy9Ec9B98y_tlKzFvxJXLY_2VHlzdz6 |
0301 dbase in c sharp data column سى شارب و قواعد البيانات | https://youtu.be/fWHgec7VGzs |
0302 dbase in c sharp datatable and datacolumn class سى شارب و قواعد البيانات | https://youtu.be/CKtyT09kSQA |
0303 dbase in c sharp data table and data column data row class سى شارب و قواعد البيانات | https://youtu.be/O5-5iDLndXY |
0304 dbase in c sharp سى شارب و قواعد البيانات EXPORT DATA TABLE TO XML FILE | https://youtu.be/1O_rCiz9DbM |
0305 dbase in c sharp سى شارب و قواعد البيانات READ XML FILE TO DATA TABLE | https://youtu.be/rq0ExYnly3g |
0306 READ XML FILE EXPORT TO XLS FILE IN C SHARP سى شارب و قواعد البيانات | https://youtu.be/GTHYs-wQI1g |
0307 Data Set DataRelation primary key column C SHARP سى شارب و قواعد البيانات جزء 1 | https://youtu.be/Cx1tGcVzISU |
0308 Data Set DataRelation primary key column C SHARP سى شارب و قواعد البيانات جزء 2 | https://youtu.be/lRYkFYooEG8 |
14 | 14 |
-3-
-4-