-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 WindowsFormsApplication27
{
public partial class Form1 : Form
{
DataSet salesSet;
DataTable orderTable;
DataTable orderDetailTable;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void createdatasetAndPut2Tables()
{
// Create two tables and add them into the DataSet
orderTable = CreateOrderTable();
orderDetailTable = CreateOrderDetailTable();
salesSet = new DataSet();
salesSet.Tables.Add(orderTable);
salesSet.Tables.Add(orderDetailTable);
}
private static DataTable CreateOrderTable()
{
DataTable orderTable = new DataTable("Order");
// Define one column.
DataColumn colId = new DataColumn("OrderId", typeof(String));
orderTable.Columns.Add(colId);
DataColumn colDate = new DataColumn("OrderDate", typeof(DateTime));
orderTable.Columns.Add(colDate);
// Set the OrderId column as the primary key.
orderTable.PrimaryKey = new DataColumn[] { colId }; //no1
// orderTable.Constraints.Add("oid_pk", orderTable.Columns["OrderId"], true); //no2
//orderTable.Constraints.Add("oid_pk", new DataColumn[] { orderTable.Columns["OrderId"], orderTable.Columns["OrderDate"] }, true); //no3
return orderTable;
}
private static DataTable CreateOrderDetailTable()
{
DataTable orderDetailTable = new DataTable("OrderDetail");
// Define all the columns once.
DataColumn[] cols ={
new DataColumn("OrderDetailId",typeof(Int32)),
new DataColumn("OrderId",typeof(String)),
new DataColumn("Product",typeof(String)),
new DataColumn("UnitPrice",typeof(Decimal)),
new DataColumn("OrderQty",typeof(Int32)),
new DataColumn("LineTotal",typeof(Decimal),"UnitPrice*OrderQty")
};
orderDetailTable.Columns.AddRange(cols);
orderDetailTable.PrimaryKey = new DataColumn[] { orderDetailTable.Columns["OrderDetailId"] };
return orderDetailTable;
}
private static void InsertOrders(DataTable orderTable)
{
// Add one row once.
DataRow row1 = orderTable.NewRow();
row1["OrderId"] = "O0001";
row1["OrderDate"] = new DateTime(2013, 3, 1);
orderTable.Rows.Add(row1);
DataRow row2 = orderTable.NewRow();
row2["OrderId"] = "O0002";
row2["OrderDate"] = new DateTime(2013, 3, 12);
orderTable.Rows.Add(row2);
DataRow row3 = orderTable.NewRow();
row3["OrderId"] = "O0003";
row3["OrderDate"] = new DateTime(2013, 3, 20);
orderTable.Rows.Add(row3);
}
private static void InsertOrderDetails(DataTable orderDetailTable)
{
// Use an Object array to insert all the rows .
// Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
Object[] rows = {
new Object[]{1,"O0001","Mountain Bike",149.5,36},
new Object[]{2,"O0001","Road Bike",1237.6,16},
new Object[]{3,"O0001","Touring Bike",1658.3,32},
new Object[]{4,"O0002","Mountain Bike",1418.5,24},
new Object[]{5,"O0002","Road Bike",1234.6,12},
new Object[]{6,"O0003","Mountain Bike",1417.5,48},
new Object[]{7,"O0003","Touring Bike",1653.3,8},
new Object[]{8,"O0003","Touring Bike",1655.13,8},
new Object[]{9,"O0003","Touring Bike1",1655.13,8}
};
foreach (Object[] row in rows)
{
orderDetailTable.Rows.Add(row);
}
}
private void BindData1()
{
// Create a BindingSource
// BindingSource bs1 = new BindingSource();
// bs1.DataSource = salesSet.Tables["Order"];
// Bind data to DataGridView.DataSource
// dataGridView1.DataSource = bs1;
dataGridView1.DataSource = orderTable;
}
private void BindData2()
{
// Create a BindingSource
// BindingSource bs2 = new BindingSource();
// bs2.DataSource = salesSet.Tables["OrderDetail"];
// Bind data to DataGridView.DataSource
dataGridView2.DataSource = orderDetailTable;
}
private void button1_Click(object sender, EventArgs e)
{
// Use the Aggregate-Sum on the child table column to get the result.
DataColumn colSub = new DataColumn("SubTotal", typeof(Decimal), "Sum(Child.LineTotal)");
orderTable.Columns.Add(colSub);
}
private void button2_Click(object sender, EventArgs e)
{
// Compute the tax by referencing the SubTotal expression column.
DataColumn colTax = new DataColumn("Tax", typeof(Decimal), "SubTotal*0.1");
orderTable.Columns.Add(colTax);
}
private void button3_Click(object sender, EventArgs e)
{
// If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
DataColumn colTotal = new DataColumn("TotalDue", typeof(Decimal), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)");
orderTable.Columns.Add(colTotal);
}
private void button4_Click(object sender, EventArgs e)
{
BindData1();
}
private void button5_Click(object sender, EventArgs e)
{
BindData2();
}
private void button6_Click(object sender, EventArgs e)
{
DataRow row = orderTable.NewRow();
row["OrderId"] = "Total";
row["OrderDate"] = new DateTime(2013, 3, 20);
orderTable.Rows.Add(row);
}
private void button7_Click(object sender, EventArgs e)
{
//object CROW= new Object[] { 4, "O0002", "Mountain Bike", 1419.5, 24 },
orderDetailTable.Rows[3][0] = "18";
orderDetailTable.Rows[3][1] = "O0003";
orderDetailTable.Rows[3][2] = "xxXxxx Bike";
orderDetailTable.Rows[3][3] = 1124;
orderDetailTable.Rows[orderDetailTable.Rows.Count - 1][3] = 1124;
}
private void button8_Click(object sender, EventArgs e)
{
//CLEAR ROWS ONLY
//orderDetailTable.Clear();
orderDetailTable.Rows.Clear();
}
private void button9_Click(object sender, EventArgs e)
{
orderDetailTable.Columns.RemoveAt(5);
orderDetailTable.Columns.RemoveAt(4);
orderDetailTable.Columns.RemoveAt(3);
orderDetailTable.Columns.RemoveAt(2);
//orderDetailTable.Columns.RemoveAt(1);
//orderDetailTable.Columns.Clear();
}
private void button10_Click(object sender, EventArgs e)
{
int rn = Convert.ToInt32(textBox1.Text);
orderDetailTable.Rows.RemoveAt(rn);
}
private void button11_Click(object sender, EventArgs e)
{
int rn = Convert.ToInt32(textBox1.Text);
orderDetailTable.Rows.Remove(orderDetailTable.Rows[rn]);
}
private void button12_Click(object sender, EventArgs e)
{
orderTable.Rows.Remove(orderTable.Rows.Find(textBox2.Text));
}
private void button13_Click(object sender, EventArgs e)
{
DataRow ff = orderTable.Rows.Find(textBox2.Text);
MessageBox.Show(ff.ItemArray[0].ToString()
+ "\t\n" + ff.ItemArray[1].ToString()
+ "\t\n" + ff.ItemArray[2].ToString()
+ "\t\n" + ff.ItemArray[3].ToString()
+ "\t\n" + ff.ItemArray[4].ToString()
);
}
private void button14_Click(object sender, EventArgs e)
{
orderTable.Rows.Remove(orderTable.Rows.Find(new object[] { "O0003", new DateTime(2013, 3, 20) }));
}
private void button15_Click(object sender, EventArgs e)
{
createdatasetAndPut2Tables();
}
private void button16_Click(object sender, EventArgs e)
{
// Set the relations between the tables and create the related constraint.
salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"], true);
}
private void button17_Click(object sender, EventArgs e)
{
// Insert the rows into the table
InsertOrders(orderTable);
InsertOrderDetails(orderDetailTable);
//========================
}
private void button18_Click(object sender, EventArgs e)
{
//orderDetailTable.Constraints.Add("OrderOrderDetailfk2", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"]);
ForeignKeyConstraint fk = new ForeignKeyConstraint("OrderOrderDetailfk1", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"]);
fk.DeleteRule = Rule.Cascade;
orderDetailTable.Constraints.Add(fk);
}
private void button19_Click(object sender, EventArgs e)
{
UniqueConstraint proNanem = new UniqueConstraint("uniqproNamr", orderDetailTable.Columns["Product"]);
orderDetailTable.Constraints.Add(proNanem);
}
private void button20_Click(object sender, EventArgs e)
{
UniqueConstraint proNanemPrice = new UniqueConstraint("uniqproNamrPrice",new DataColumn[] { orderDetailTable.Columns["Product"], orderDetailTable.Columns["UnitPrice"] });
orderDetailTable.Constraints.Add(proNanemPrice);
}
}
}
اسم الملف | لينك |
---|---|
تعلم لغة 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 |
0309 Data Set Good example C SHARP سى شارب و قواعد البيانات part1 | https://youtu.be/_GN6P1RS7SI |
0310 Data Set Good example C SHARP سى شارب و قواعد البيانات part2 | https://youtu.be/SRXel5En2JA |
0311 Data Set Good example C SHARP سى شارب و قواعد البيانات part3 | https://youtu.be/AGn887sz4as |
17 | 17 |
-3-
-4-