80 likes | 89 Views
購物範例. 步驟一:建立 UI. 一個 combobox + ㄧ個 TextBox+ ㄧ個 Gridview 物件 二個 Button 物件. Form_Load. ‘ 本表單全域物件 Dim dt As New DataTable("") dt.Columns.Add(" 產品編號 ") dt.Columns.Add(" 產品名稱 ") dt.Columns.Add(" 數量 "). 設定 ComboBox. 設定 ComboBox. 設定 ComboBox. 設定 ComboBox.
E N D
步驟一:建立UI 一個combobox + ㄧ個TextBox+ㄧ個Gridview 物件 二個Button物件
Form_Load ‘ 本表單全域物件 Dim dt As New DataTable("") dt.Columns.Add("產品編號") dt.Columns.Add("產品名稱") dt.Columns.Add("數量")
設定ComboBox Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged p_id = ComboBox1.SelectedValue.ToString() p_name = ComboBox1.Text End Sub Dim p_id, p_name As String ‘ 加入按鈕 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click dt.Rows.Add(New Object() {p_id, p_name, TextBox1.Text}) DataGridView1.DataSource = dt End Sub
確定購買 Dim i As Integer For i = 1 To DataGridView1.RowCount - 1 'DataGridView1.Rows(i - 1).Cells(0).Value.ToString() ' 一.connection物件 '1.宣告 Dim dbconn As OleDbConnection '2.產生物件並給定參數 dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=teacher.mdb;User Id=admin;Password=;") dbconn.Open() '二.command物件執行sql指令 '1.宣告 Dim dbcmd As New OleDbCommand '2.產生並執行sql指令 dbcmd.CommandType = CommandType.Text dbcmd.CommandText = "insert into sales (c_id,p_id,amount,s_day) values (1," + DataGridView1.Rows(i - 1).Cells(0).Value + "," + DataGridView1.Rows(i - 1).Cells(2).Value + ",'" + System.DateTime.Today.ToString() + "')" dbcmd.Connection = dbconn dbcmd.ExecuteNonQuery() dbconn.Close() Next