Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Microsoft.VisualBasic;
  9. //
  10. //加入參考 Mircosoft.VisualBasic,
  11. //就可以使用Microsoft.VisualBasic.Interaction.InputBox("測試", "標頭", "預設值", 400, 300);
  12.  
  13. namespace WindowsFormsApplication2
  14. {
  15. public partial class Form1 : Form
  16. {
  17.  
  18. public Form1()
  19. {
  20.  
  21. InitializeComponent();
  22. }
  23.  
  24. public void Form1_Load(object sender, EventArgs e)
  25. {
  26. button1.Text = "建立DataSet";
  27. //button2.Text = "修改商品資料表的紀錄";
  28.  
  29.  
  30.  
  31.  
  32. }
  33.  
  34. public void button1_Click(object sender, EventArgs e)
  35. {
  36. //預設 開始建立 DataSet
  37. DataSet dsKings = new DataSet("KINGS");
  38. //new 一個 DataSet
  39. DataTable dtProduct = new DataTable("Product");
  40. // new 一個 Table
  41.  
  42. dsKings.Tables.Add(dtProduct);
  43. //將 Table 加入 DataSet
  44.  
  45. int i;
  46. for (i = 0; i <= dsKings.Tables.Count - 1; i++)
  47. {
  48. listBox1.Items.Add(dsKings.Tables[i].TableName);
  49. }
  50.  
  51. //new Column
  52. DataColumn colProductID = new DataColumn("ProductID");
  53. colProductID.DataType = System.Type.GetType("System.String");
  54. colProductID.MaxLength = 10;
  55. colProductID.AllowDBNull = false;
  56. //商品編號
  57.  
  58. DataColumn colProductName = new DataColumn("ProductName");
  59. colProductName.DataType = System.Type.GetType("System.String");
  60. colProductName.MaxLength = 40;
  61. colProductName.AllowDBNull = false;
  62. colProductName.Unique = true;
  63. //商品編號
  64.  
  65. DataColumn colAuthor = new DataColumn("Author");
  66. colAuthor.DataType = System.Type.GetType("System.String");
  67. colAuthor.MaxLength = 12;
  68. colAuthor.AllowDBNull = false;
  69. //作者
  70.  
  71. DataColumn colStore = new DataColumn("Store");
  72. colStore.DataType = System.Type.GetType("System.String");
  73. colStore.MaxLength = 20;
  74. colStore.AllowDBNull = false;
  75. //出版社
  76.  
  77. DataColumn colTotalOrderQuantity = new DataColumn("TotalOrderQuantity");
  78. colTotalOrderQuantity.DataType = System.Type.GetType("System.String");
  79. colTotalOrderQuantity.DefaultValue = 0;
  80. colTotalOrderQuantity.AllowDBNull = false;
  81. //累積訂購量
  82.  
  83.  
  84.  
  85. dtProduct.Columns.Add(colProductID);
  86. dtProduct.Columns.Add(colProductName);
  87. dtProduct.Columns.Add(colAuthor);
  88. dtProduct.Columns.Add(colStore);
  89. dtProduct.Columns.Add(colTotalOrderQuantity);
  90. //將 Column 加入 Table
  91.  
  92. dtProduct.PrimaryKey = new DataColumn[] { dtProduct.Columns["ProductID"] };
  93. // set PrimaryKey
  94.  
  95. foreach (Constraint ProductConstraint in dtProduct.Constraints)
  96. {
  97. listBox1.Items.Add("條件約束:" + ProductConstraint.ConstraintName.ToString());
  98.  
  99. }
  100.  
  101. DataRow drNewProduct;
  102. //建立DataRow物件
  103.  
  104. //新增方式 A
  105. drNewProduct = dtProduct.NewRow();
  106. //再新的DataRow給予值
  107. drNewProduct["ProductID"] = "P2297";
  108. drNewProduct["ProductName"] = "Visual Basic .NET 近銷存程式設計01-A";
  109. drNewProduct["Author"] = "阿惟01-A";
  110. drNewProduct["Store"] = "文魁資訊01-A";
  111. dtProduct.Rows.Add(drNewProduct);
  112. //將 DataRow 加入資料表
  113.  
  114. //新增方式 B
  115. drNewProduct = dtProduct.NewRow();
  116. //再新的DataRow給予值
  117. drNewProduct[0] = "P7117";
  118. drNewProduct[1] = "Visual Basic 2005 資料庫程式設計02-A";
  119. drNewProduct[2] = "阿惟02-A";
  120. drNewProduct[3] = "文魁資訊02-A";
  121. dtProduct.Rows.Add(drNewProduct);
  122. //將 DataRow 加入資料表
  123.  
  124. //新增方式 C
  125. dtProduct.Rows.Add(new object[] { "P999", "Access 2007近銷存管理系統實作03-A", "阿惟03-A", "文魁資訊03-A" });
  126.  
  127.  
  128.  
  129. //ReLoadDataGridView();
  130.  
  131. dataGridView1.DataSource = dsKings;
  132. //用 datagridview1 接 DataSet
  133. dataGridView1.DataMember = "Product";
  134. //選定 Table
  135.  
  136.  
  137.  
  138. //修改資料表
  139. MessageBox.Show("修改資料表");
  140.  
  141. DataRow drModifyProduct;
  142. string DefaultProductID;
  143. string DefaultProductName;
  144. string OldModifyProductID;
  145. string ModifyProductID;
  146. string ModifyProductName;
  147.  
  148.  
  149.  
  150. OldModifyProductID = Interaction.InputBox("請輸入要修改的書號", "尋找修改商品的數號", "", 400, 300);
  151. if (OldModifyProductID == "")
  152. {
  153. return;
  154. }
  155. else
  156. {
  157. drModifyProduct = dsKings.Tables["Product"].Rows.Find(OldModifyProductID.ToString());
  158. }
  159.  
  160. if (drModifyProduct == null)
  161. {
  162. MessageBox.Show("沒找到要修改的東西");
  163. }
  164.  
  165. DefaultProductID = drModifyProduct["ProductID"].ToString();
  166. DefaultProductName = drModifyProduct["ProductName"].ToString();
  167. //輸入修改的值
  168. ModifyProductID = Microsoft.VisualBasic.Interaction.InputBox("請輸入" + OldModifyProductID.ToString() + "要修改成新的書號?",
  169. "商品資料表修改", DefaultProductID, 400, 300);
  170. ModifyProductName = Microsoft.VisualBasic.Interaction.InputBox("請輸入" + OldModifyProductID.ToString() + "要修改成新的書號?",
  171. "商品資料表修改", DefaultProductName, 400, 300);
  172. if (ModifyProductID == "")
  173. {
  174. ModifyProductID = DefaultProductID;
  175. }
  176.  
  177. if (ModifyProductName == "")
  178. {
  179. ModifyProductName = DefaultProductName;
  180. }
  181. //沒新值,以舊值帶入
  182.  
  183. drModifyProduct.BeginEdit();
  184. //修改欄位的值
  185. drModifyProduct["ProductID"] = ModifyProductID;
  186. drModifyProduct["ProductName"] = ModifyProductName;
  187.  
  188. drModifyProduct.EndEdit();
  189. //儲存變更
  190.  
  191.  
  192.  
  193.  
  194. dataGridView1.Refresh();
  195. //dataGridView1重新載入 資料
  196.  
  197. }
  198.  
  199.  
  200. }
  201. }
Add Comment
Please, Sign In to add comment